pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r2568 - in trunk: . src src/editor


From: jave27
Subject: [Pingus-CVS] r2568 - in trunk: . src src/editor
Date: Thu, 29 Dec 2005 19:55:19 +0100

Author: jave27
Date: 2005-12-29 19:55:06 +0100 (Thu, 29 Dec 2005)
New Revision: 2568

Added:
   trunk/src/editor/context_menu.cxx
   trunk/src/editor/context_menu.hxx
Modified:
   trunk/Pingus.vcproj
   trunk/src/editor/Makefile.am
   trunk/src/editor/editor_viewport.cxx
   trunk/src/editor/editor_viewport.hxx
   trunk/src/editor/level_objs.cxx
   trunk/src/editor/level_objs.hxx
   trunk/src/file_dialog.cxx
Log:
Added a mostly-functional context menu to the editor.

Made the refresh_sprite() function work.

Made the file dialog display the filename if the description was blank.


Modified: trunk/Pingus.vcproj
===================================================================
--- trunk/Pingus.vcproj 2005-12-28 18:58:16 UTC (rev 2567)
+++ trunk/Pingus.vcproj 2005-12-29 18:55:06 UTC (rev 2568)
@@ -1697,6 +1697,12 @@
                                Name="editor"
                                Filter="">
                                <File
+                                       
RelativePath=".\src\editor\context_menu.cxx">
+                               </File>
+                               <File
+                                       
RelativePath=".\src\editor\context_menu.hxx">
+                               </File>
+                               <File
                                        
RelativePath=".\src\editor\editor_panel.cxx">
                                </File>
                                <File

Modified: trunk/src/editor/Makefile.am
===================================================================
--- trunk/src/editor/Makefile.am        2005-12-28 18:58:16 UTC (rev 2567)
+++ trunk/src/editor/Makefile.am        2005-12-29 18:55:06 UTC (rev 2568)
@@ -22,6 +22,7 @@
 
 libpingus_editor_a_CPPFLAGS = @PINGUS_CFLAGS@
 libpingus_editor_a_SOURCES =       \
+context_menu.cxx       context_menu.hxx        \
 editor_screen.cxx      editor_screen.hxx       \
 editor_panel.cxx       editor_panel.hxx        \
 panel_buttons.cxx      panel_buttons.hxx       \

Added: trunk/src/editor/context_menu.cxx
===================================================================
--- trunk/src/editor/context_menu.cxx   2005-12-28 18:58:16 UTC (rev 2567)
+++ trunk/src/editor/context_menu.cxx   2005-12-29 18:55:06 UTC (rev 2568)
@@ -0,0 +1,118 @@
+//  $Id: context_menu.cxx,v 1.00 2005/12/29 23:41:12 Jave27 Exp $
+//
+//  Pingus - A free Lemmings clone
+//  Copyright (C) 2005 Ingo Ruhnke <address@hidden>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#include <ClanLib/Display/sprite.h>
+#include "context_menu.hxx"
+#include "level_objs.hxx"
+#include "editor_viewport.hxx"
+#include "../fonts.hxx"
+
+namespace Pingus {
+
+namespace Editor {
+
+       // Determine which actions are available for these objects
+       ContextMenu::ContextMenu(std::vector<LevelObj*> o, Vector p, 
EditorViewport* vp)
+               : objs(o), 
+                       viewport(vp),
+                       pos(p),
+                       width(200),
+                       item_height(Fonts::pingus_small.get_height()),
+                       selected_action_offset(0)
+       {
+               actions.push_back("Remove");
+               actions.push_back("ROT0");
+               actions.push_back("ROT90");
+               actions.push_back("ROT180");
+               actions.push_back("ROT270");
+
+               total_height  = item_height * actions.size();
+       }
+
+
+       ContextMenu::~ContextMenu()
+       {
+       }
+
+
+       // Keep track of where the mouse is for highlighting
+       void
+       ContextMenu::on_pointer_move(int x, int y)
+       {
+               mouse_at.x = (float)x; 
+               mouse_at.y = (float)y;
+
+               selected_action_offset = (unsigned)((mouse_at.y - pos.y) / 
item_height);
+       }
+
+       void
+       ContextMenu::draw(DrawingContext &gc)
+       {
+               // Draw the box
+               gc.draw_fillrect(pos.x, pos.y, pos.x + 200, pos.y + 
total_height, 
+                       CL_Color(211,211,211,100));
+               // Draw the border
+               gc.draw_rect(pos.x, pos.y, pos.x + 200, pos.y + total_height, 
+                       CL_Color::black);
+               // Draw the highlighted action if the mouse is in the box
+               if (hover)
+                       gc.draw_fillrect(pos.x, pos.y + selected_action_offset 
* 
+                               item_height, pos.x + 200, pos.y + 
(selected_action_offset + 1) * item_height,
+                               CL_Color(128,128,128,100));
+
+               // Draw the action names
+               for (unsigned i = 0; i < actions.size(); i++)
+                       gc.print_left(Fonts::pingus_small, pos.x, pos.y + 
+                               (i * item_height), actions[i]);
+       }
+
+       bool
+       ContextMenu::is_at(int x, int y)
+       {
+               return (x > pos.x && x < pos.x + 200 &&
+                       y > pos.y && y < pos.y + total_height);
+       }
+
+       void 
+       ContextMenu::on_primary_button_click(int x, int y)
+       {
+               // FIXME: Call the correct object function based on the 
selected action.
+               // FIXME: This is a temporary hack to test the functionality.
+               for (unsigned i = 0; i < objs.size(); i++)
+               {
+                       if (selected_action_offset == 0)
+                               objs[i]->remove();
+                       else
+                               
objs[i]->set_modifier(actions[selected_action_offset]);
+               }
+               viewport->remove_context_menu();
+       }
+
+       void
+       ContextMenu::on_secondary_button_click(int x, int y)
+       {
+               // Does the same as the primary button
+               on_primary_button_click(x, y);
+       }
+
+}      // Editor namespace
+} // Pingus namespace
+
+/* EOF */
+

Added: trunk/src/editor/context_menu.hxx
===================================================================
--- trunk/src/editor/context_menu.hxx   2005-12-28 18:58:16 UTC (rev 2567)
+++ trunk/src/editor/context_menu.hxx   2005-12-29 18:55:06 UTC (rev 2568)
@@ -0,0 +1,91 @@
+//  $Id: context_menu.hxx,v 1.00 2005/12/29 23:41:12 Jave27 Exp $
+//
+//  Pingus - A free Lemmings clone
+//  Copyright (C) 2005 Ingo Ruhnke <address@hidden>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#ifndef HEADER_PINGUS_EDITOR_CONTEXT_MENU_HXX
+#define HEADER_PINGUS_EDITOR_CONTEXT_MENU_HXX
+
+#include <vector>
+#include "../vector.hxx"
+#include "../gui/component.hxx"
+
+namespace Pingus {
+
+namespace Editor {
+
+       class LevelObj;
+       class EditorViewport;
+
+       class ContextMenu : public GUI::Component {
+       private:
+               /** Level objects to be affected by this menu */
+               std::vector<LevelObj*> objs;
+
+               /** Viewport to which this menu belongs */
+               EditorViewport* viewport;
+
+               /** List of actions available in this menu */
+               std::vector<std::string> actions;
+
+               /** Where the mouse is located */
+               Vector mouse_at;
+
+               /** Location of context menu */
+               Vector pos;
+
+               /** Is the mouse over the menu? */
+               bool hover;
+
+               /** The offset into actions vector of the currently highlighted 
action */
+               unsigned selected_action_offset;
+
+               /** Height of a single action */
+               unsigned item_height;
+
+               unsigned total_height;
+               unsigned width;
+
+       public:
+               // Constructor
+               ContextMenu (std::vector<LevelObj*>, Vector p, EditorViewport* 
v);
+               
+               // Desctructor
+               ~ContextMenu ();
+
+               /// GUI Component Functions
+               bool is_at(int x, int y);
+               void draw (DrawingContext& gc);
+               void on_pointer_move (int x, int y);
+               void on_primary_button_click(int x, int y);
+               void on_secondary_button_click(int x, int y);
+               void on_pointer_enter () { hover = true; }
+               void on_pointer_leave () { hover = false; }
+
+       private:
+               ContextMenu ();
+               ContextMenu (const ContextMenu&);
+         ContextMenu& operator= (const ContextMenu&);
+
+       };      // ContextMenu class
+
+}      // Editor namespace
+} // Pingus namespace
+
+#endif
+
+/* EOF */

Modified: trunk/src/editor/editor_viewport.cxx
===================================================================
--- trunk/src/editor/editor_viewport.cxx        2005-12-28 18:58:16 UTC (rev 
2567)
+++ trunk/src/editor/editor_viewport.cxx        2005-12-29 18:55:06 UTC (rev 
2568)
@@ -21,11 +21,13 @@
 #include <vector>
 #include <string>
 #include <iostream>
+#include "../gui/gui_manager.hxx"
+#include "../display/drawing_context.hxx"
 #include "../vector.hxx"
-#include "../display/drawing_context.hxx"
 #include "../graphic_context_state.hxx"
 #include "editor_viewport.hxx"
 #include "editor_screen.hxx"
+#include "context_menu.hxx"
 #include "xml_level.hxx"
 #include "level_objs.hxx"
 
@@ -38,6 +40,7 @@
        scene_context(new SceneContext()),
        editor(e),
        current_obj(0),
+       context_menu(0),
        snap_to(true),
        autoscroll(true)
 {
@@ -55,19 +58,30 @@
 void
 EditorViewport::on_secondary_button_click(int x, int y)
 {
-       std::cout << "Right-click at " << x - (state.get_width()/2 - 
state.get_pos().x) << ", " 
-               << y - (state.get_height()/2 - state.get_pos().y) << std::endl;
+       remove_context_menu();
+
+       Vector mouse_pos(x - (state.get_width()/2 - state.get_pos().x),
+               y - (state.get_height()/2 - state.get_pos().y));
+
+       std::cout << "Right-click at " << mouse_pos.x << ", " 
+               << mouse_pos.y << std::endl;
        
-       LevelObj* obj = object_at(x - (state.get_width()/2 - 
(int)state.get_pos().x),
-               y - (state.get_height()/2 - (int)state.get_pos().y));
+       LevelObj* obj = object_at((int)mouse_pos.x, (int)mouse_pos.y);
        if (obj)
-               obj->on_primary_button_click(x, y);
+       {
+               std::vector<LevelObj*> objs;
+               objs.push_back(obj);
+               context_menu = new ContextMenu(objs, Vector((float)x, 
(float)y), this);
+               editor->get_gui_manager()->add(context_menu, true);
+       }
 }
 
 // Select a LevelObj
 void 
 EditorViewport::on_primary_button_press(int x, int y)
 {
+       remove_context_menu();
+
        LevelObj* obj = object_at(x - (state.get_width()/2 - 
(int)state.get_pos().x),
                y - (state.get_height()/2 - (int)state.get_pos().y));
        if (obj)
@@ -166,6 +180,17 @@
        return 0;
 }
 
+// Delete the context menu if it exists.
+void
+EditorViewport::remove_context_menu()
+{
+       if (context_menu)
+       {
+               editor->get_gui_manager()->remove(context_menu);
+               context_menu = 0;
+       }
+}
+
 } // Editor namespace
 } // Pingus namespace
 

Modified: trunk/src/editor/editor_viewport.hxx
===================================================================
--- trunk/src/editor/editor_viewport.hxx        2005-12-28 18:58:16 UTC (rev 
2567)
+++ trunk/src/editor/editor_viewport.hxx        2005-12-29 18:55:06 UTC (rev 
2568)
@@ -35,6 +35,7 @@
 
        class LevelObj;
        class EditorScreen;
+       class ContextMenu;
 
 /** This class is where the actual level graphics will display in the
        level editor.  Objects can be added, deleted, moved, modified, etc. 
@@ -65,6 +66,9 @@
     coordinates */
   void on_pointer_move (int x, int y);
 
+       /** Get rid of context menu if it exists */
+       void remove_context_menu();
+
 private:
        EditorViewport();
        EditorViewport (const EditorViewport&);
@@ -91,6 +95,9 @@
        /** Returns the topmost object at this x, y location */
        LevelObj* object_at(int x, int y);
 
+       /** There should only be 0 or 1 context menus on the screen */
+       ContextMenu* context_menu;
+
        /** Whether or not the "snap-to-grid" functionality is on. */
        bool snap_to;
 

Modified: trunk/src/editor/level_objs.cxx
===================================================================
--- trunk/src/editor/level_objs.cxx     2005-12-28 18:58:16 UTC (rev 2567)
+++ trunk/src/editor/level_objs.cxx     2005-12-29 18:55:06 UTC (rev 2568)
@@ -47,6 +47,7 @@
        para_x(0),
        para_y(0),
        color(0,0,0,0),
+       removed(false),
        attribs(get_attributes(obj_name))
 {
        
@@ -63,7 +64,7 @@
 void
 LevelObj::draw(DrawingContext &gc)
 {
-       if (attribs & HAS_SURFACE)
+       if (!removed && attribs & HAS_SURFACE)
        {
                if (attribs & HAS_WIDTH)
                {
@@ -86,7 +87,7 @@
 bool
 LevelObj::is_at(int x, int y)
 {
-       if (attribs & HAS_SURFACE)
+       if (!removed && attribs & HAS_SURFACE)
                return (x > pos.x && x < pos.x + sprite.get_width()
                        && y > pos.y && y < pos.y + sprite.get_height());
        else
@@ -109,42 +110,60 @@
 LevelObj::set_stretch_x(const bool s)
 { 
        stretch_x = s;
-       refresh_sprite();
 }
 
 void
 LevelObj::set_stretch_y(const bool s)
 { 
        stretch_y = s;
-       refresh_sprite();
 }
 
 void
 LevelObj::set_aspect(const bool a)
 { 
        keep_aspect = a;
-       refresh_sprite();
 }
 
 void
 LevelObj::refresh_sprite()
 {
+       CL_PixelBuffer pb;
+
        // Apply modifier, then change the sprite loaded for this object in 
memory.
-       sprite = Resource::load_sprite(desc);
-
-       if (0)
-       //if (stretch_x || stretch_y)
+       if (stretch_x || stretch_y)
        {
-               float w, h;
-               // FIXME: Temporary hack
-               w = 800;
-               h = 600;
+               sprite = Resource::load_sprite(desc);
+               float w = (float)sprite.get_width();
+               float h = (float)sprite.get_height();
+               
+               // Determine the new dimensions for the sprite
+               if (stretch_x && !stretch_y)
+               {
+                       if (keep_aspect)
+                               h = h * CL_Display::get_width() / w;
+                       w = (float)CL_Display::get_width();
+               }
+               else if (stretch_y && !stretch_x)
+               {
+                       if (keep_aspect)
+                               w = w * CL_Display::get_height() / h;
+                       h = (float)CL_Display::get_height();
+               }
+               else
+               {
+                       w = (float)CL_Display::get_width();
+                       h = (float)CL_Display::get_height();
+               }
 
-               CL_Surface sur = 
Blitter::scale_surface_to_canvas(sprite.get_frame_surface(0), (int)w, (int)h);
-               CL_SpriteDescription sprite_desc;
-               sprite_desc.add_frame(sur.get_pixeldata());
-               sprite = CL_Sprite(sprite_desc);
+               pb = Blitter::scale_surface_to_canvas(
+                       sprite.get_frame_pixeldata(0), (int)w, (int)h);
        }
+       else            // No stretch involved
+               pb = Resource::load_pixelbuffer(desc);
+
+       CL_SpriteDescription sprite_desc;
+       sprite_desc.add_frame(pb);
+       sprite = CL_Sprite(sprite_desc);
 }
 
 // Set the modifier and actually modify the sprite loaded in memory
@@ -160,59 +179,62 @@
 void
 LevelObj::write_properties(XMLFileWriter &xml)
 {
-       xml.begin_section(section_name.c_str());
+       if (!removed)
+       {
+               xml.begin_section(section_name.c_str());
 
-       const unsigned attribs = get_attributes(section_name);
+               const unsigned attribs = get_attributes(section_name);
 
-       // Write information about the main sprite
-       if (attribs & HAS_SURFACE)
-       {
-               xml.begin_section("surface");
-               xml.write_string("image", desc.res_name);
-               xml.write_string("modifier", 
ResourceModifierNS::rs_to_string(desc.modifier));
-               xml.end_section();      // surface
-       }
-       // Write the optional information
-       if (attribs & HAS_TYPE)
-               xml.write_string("type", object_type);
-       if (attribs & HAS_SPEED)
-               xml.write_int("speed", speed);
-       if (attribs & HAS_PARALLAX)
-               xml.write_float("parallax", parallax);
-       if (attribs & HAS_WIDTH)
-               xml.write_int("width", width);
-       if (attribs & HAS_OWNER)
-               xml.write_int("owner-id", owner_id);
-       if (attribs & HAS_DIRECTION)
-               xml.write_string("direction", direction);
-       if (attribs & HAS_RELEASE_RATE)
-               xml.write_int("release-rate", release_rate);
-       if (attribs & HAS_COLOR)
-               xml.write_color("color", color);
-       if (attribs & HAS_STRETCH)
-       {
-               xml.write_bool("stretch-x", stretch_x);
-               xml.write_bool("stretch-y", stretch_y);
-               xml.write_bool("keep-aspect", keep_aspect);
-       }
-       if (attribs & HAS_SCROLL)
-       {
-               xml.write_float("scroll-x", scroll_x);
-               xml.write_float("scroll-y", scroll_y);
-       }
-       if (attribs & HAS_PARA)
-       {
-               xml.write_float("para-x", para_x);
-               xml.write_float("para-y", para_y);
-       }
+               // Write information about the main sprite
+               if (attribs & HAS_SURFACE)
+               {
+                       xml.begin_section("surface");
+                       xml.write_string("image", desc.res_name);
+                       xml.write_string("modifier", 
ResourceModifierNS::rs_to_string(desc.modifier));
+                       xml.end_section();      // surface
+               }
+               // Write the optional information
+               if (attribs & HAS_TYPE)
+                       xml.write_string("type", object_type);
+               if (attribs & HAS_SPEED)
+                       xml.write_int("speed", speed);
+               if (attribs & HAS_PARALLAX)
+                       xml.write_float("parallax", parallax);
+               if (attribs & HAS_WIDTH)
+                       xml.write_int("width", width);
+               if (attribs & HAS_OWNER)
+                       xml.write_int("owner-id", owner_id);
+               if (attribs & HAS_DIRECTION)
+                       xml.write_string("direction", direction);
+               if (attribs & HAS_RELEASE_RATE)
+                       xml.write_int("release-rate", release_rate);
+               if (attribs & HAS_COLOR)
+                       xml.write_color("color", color);
+               if (attribs & HAS_STRETCH)
+               {
+                       xml.write_bool("stretch-x", stretch_x);
+                       xml.write_bool("stretch-y", stretch_y);
+                       xml.write_bool("keep-aspect", keep_aspect);
+               }
+               if (attribs & HAS_SCROLL)
+               {
+                       xml.write_float("scroll-x", scroll_x);
+                       xml.write_float("scroll-y", scroll_y);
+               }
+               if (attribs & HAS_PARA)
+               {
+                       xml.write_float("para-x", para_x);
+                       xml.write_float("para-y", para_y);
+               }
 
-       // Writes any extra properties that may be necessary (virtual function)
-       write_extra_properties(xml);
+               // Writes any extra properties that may be necessary (virtual 
function)
+               write_extra_properties(xml);
 
-       // Write the Vector position - all objects have this
-       xml.write_vector("position", pos);
+               // Write the Vector position - all objects have this
+               xml.write_vector("position", pos);
 
-       xml.end_section();      // object's section_name
+               xml.end_section();      // object's section_name
+       }
 }
 
 }              // Editor namespace

Modified: trunk/src/editor/level_objs.hxx
===================================================================
--- trunk/src/editor/level_objs.hxx     2005-12-28 18:58:16 UTC (rev 2567)
+++ trunk/src/editor/level_objs.hxx     2005-12-29 18:55:06 UTC (rev 2568)
@@ -137,14 +137,13 @@
        /** Number representing which attributes this object possesses */
        unsigned attribs;
 
+       /** Marks if this object has been deleted or not */
+       bool removed;
+
        /** Write any additional properties to the XML file for this type */
        virtual void write_extra_properties(XMLFileWriter& xml) { }
 
-       /** Call when the sprite needs to be reloaded */
-       void refresh_sprite();
 
-
-
 /////////////////////////////////////////////////////////
 /// Retrieve info
 public:
@@ -268,9 +267,15 @@
        /** Set the object's direction if applicable */
        void set_direction(const std::string d) { direction = d; }
 
+       /** Soft delete of the object (needed for Undo action) */
+       void remove() { removed = true; }
+
        /** Write basic properties to the XML file for this type */
        virtual void write_properties(XMLFileWriter &xml);
 
+       /** Call when the sprite needs to be reloaded */
+       void refresh_sprite();
+
        /** Draws the sprite with the modifier applied */
        virtual void draw(DrawingContext &gc);
 

Modified: trunk/src/file_dialog.cxx
===================================================================
--- trunk/src/file_dialog.cxx   2005-12-28 18:58:16 UTC (rev 2567)
+++ trunk/src/file_dialog.cxx   2005-12-29 18:55:06 UTC (rev 2568)
@@ -279,7 +279,7 @@
                gc.draw_rect(gc.get_width() / 2 - 285, gc.get_height() / 2 - 
160,
                        gc.get_width() / 2 + 285, gc.get_height() / 2 + 160, 
CL_Color::black);
                gc.print_center(Fonts::chalk_large, gc.get_width()/2, 
gc.get_height()/2 - 220, 
-                       current_file.friendly_name);
+                       current_file.friendly_name == "" ? current_file.name : 
current_file.friendly_name);
 
                PingusSubMenu::draw(gc);
                return true;





reply via email to

[Prev in Thread] Current Thread [Next in Thread]