Browse Source

Implement scrolling in the GTK window using the scrollbars.

legacy
Sam Hocevar sam 14 years ago
parent
commit
324013d8dc
4 changed files with 109 additions and 21 deletions
  1. +48
    -9
      src/gtk/editor.cpp
  2. +43
    -6
      src/gtk/editor.xml
  3. +15
    -5
      src/mapviewer.cpp
  4. +3
    -1
      src/mapviewer.h

+ 48
- 9
src/gtk/editor.cpp View File

@@ -22,6 +22,10 @@ static volatile int quit = 0;
static int ticking = 0;
static float const FPS = 30.0f;

static MapViewer *mv;
static GtkWidget *glarea;
static GtkAdjustment *hadj, *vadj;

static gint main_quit(GtkWidget *widget, GdkEventExpose *event)
{
(void)widget;
@@ -42,6 +46,8 @@ static gboolean tick(void *widget)

ticking = 1;

mv->SetPOV(gtk_adjustment_get_value(hadj), gtk_adjustment_get_value(vadj));

/* Tick the game */
Ticker::TickGame();

@@ -52,13 +58,40 @@ static gboolean tick(void *widget)

static gint init(GtkWidget *widget)
{
struct
{
GtkAdjustment *adj;
float map_size, sw_size;
}
s[2] =
{
{ hadj, mv->GetWidth(), widget->allocation.width },
{ vadj, mv->GetHeight(), widget->allocation.height },
};

for (int i = 0; i < 2; i++)
{
gtk_adjustment_set_lower(s[i].adj, 0);
gtk_adjustment_set_upper(s[i].adj, s[i].map_size);
gtk_adjustment_set_step_increment(s[i].adj, 1);
gtk_adjustment_set_page_increment(s[i].adj, s[i].sw_size);
gtk_adjustment_set_page_size(s[i].adj, s[i].sw_size);

float hval = gtk_adjustment_get_value(s[i].adj);
if (hval + s[i].sw_size > s[i].map_size)
{
gtk_adjustment_set_value(s[i].adj, s[i].map_size - s[i].sw_size);
gtk_adjustment_value_changed(s[i].adj);
}
}

if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
Video::Setup(widget->allocation.width, widget->allocation.height);

return TRUE;
}

static gint reshape(GtkWidget *widget, GdkEventConfigure *event)
static gint reshape_gl(GtkWidget *widget, GdkEventConfigure *event)
{
(void)event;

@@ -100,7 +133,7 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

/* Build the rest of the application */
/* Build the application interface and keep a few member pointers */
GtkBuilder *builder = gtk_builder_new();
if (!gtk_builder_add_from_file(builder, "src/gtk/editor.xml", NULL))
{
@@ -109,7 +142,12 @@ int main(int argc, char **argv)
}

GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
GtkWidget *sw = GTK_WIDGET(gtk_builder_get_object(builder, "sw1"));
GtkWidget *viewport = GTK_WIDGET(
gtk_builder_get_object(builder, "viewport1"));
hadj = gtk_range_get_adjustment(GTK_RANGE(
gtk_builder_get_object(builder, "hscrollbar1")));
vadj = gtk_range_get_adjustment(GTK_RANGE(
gtk_builder_get_object(builder, "vscrollbar1")));
g_object_unref(G_OBJECT(builder));

/* Create new OpenGL widget */
@@ -123,29 +161,30 @@ int main(int argc, char **argv)
GDK_GL_NONE
};

GtkWidget *glarea = gtk_gl_area_new(attrlist);
glarea = gtk_gl_area_new(attrlist);
gtk_widget_set_usize(glarea, 400, 300);
gtk_widget_set_events(glarea, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), glarea);
gtk_container_add(GTK_CONTAINER(viewport), glarea);

/* Connect signals and show window */
gtk_signal_connect(GTK_OBJECT(window), "delete_event",
GTK_SIGNAL_FUNC(main_quit), NULL);

gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
GTK_SIGNAL_FUNC(draw), NULL);
gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
GTK_SIGNAL_FUNC(reshape), NULL);
GTK_SIGNAL_FUNC(reshape_gl), NULL);
gtk_signal_connect(GTK_OBJECT(glarea), "realize",
GTK_SIGNAL_FUNC(init), NULL);

gtk_widget_show_all(window);

// FIXME: detect when the map viewer is killed
new MapViewer("maps/testmap.tmx");
mv = new MapViewer("maps/testmap.tmx");
new DebugFps();

/* We tick from the idle function instead of a timeout to avoid
* stealing time from the GTK loop when the callback time exceeds
* the timeout value. */
gtk_widget_show_all(window);
gtk_idle_add(tick, glarea);
gtk_main();



+ 43
- 6
src/gtk/editor.xml View File

@@ -153,11 +153,47 @@
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<object class="GtkScrolledWindow" id="sw1">
<property name="width_request">640</property>
<property name="height_request">480</property>
<object class="GtkTable" id="table2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="n_rows">2</property>
<property name="n_columns">2</property>
<child>
<object class="GtkHScrollbar" id="hscrollbar1">
<property name="visible">True</property>
</object>
<packing>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="y_options">GTK_SHRINK</property>
</packing>
</child>
<child>
<object class="GtkVScrollbar" id="vscrollbar1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="x_options">GTK_SHRINK</property>
</packing>
</child>
<child>
<object class="GtkViewport" id="viewport1">
<property name="visible">True</property>
<property name="resize_mode">queue</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="x_options">GTK_EXPAND | GTK_SHRINK | GTK_FILL</property>
<property name="y_options">GTK_EXPAND | GTK_SHRINK | GTK_FILL</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="resize">False</property>
@@ -166,12 +202,13 @@
</child>
<child>
<object class="GtkLabel" id="label1">
<property name="width_request">150</property>
<property name="visible">True</property>
<property name="label" translatable="yes">Palette will be here </property>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
<property name="resize">False</property>
<property name="shrink">False</property>
</packing>
</child>
</object>


+ 15
- 5
src/mapviewer.cpp View File

@@ -22,7 +22,7 @@ class MapViewerData
private:
Map *map;
int x, y;
int mousex, mousey;
int povx, povy;
int done;

Scene *scene;
@@ -63,7 +63,7 @@ void MapViewer::TickDraw(float deltams)

GetScene();

data->map->Render(data->scene, -data->mousex, -data->mousey, 0);
data->map->Render(data->scene, -data->povx, -data->povy, 0);
data->scene->Render();

delete data->scene;
@@ -77,9 +77,19 @@ Scene *MapViewer::GetScene()
return data->scene;
}

void MapViewer::SetMouse(int x, int y)
int MapViewer::GetWidth()
{
data->mousex = x;
data->mousey = y;
return data->map->GetWidth();
}

int MapViewer::GetHeight()
{
return data->map->GetHeight();
}

void MapViewer::SetPOV(int x, int y)
{
data->povx = x;
data->povy = y;
}


+ 3
- 1
src/mapviewer.h View File

@@ -31,7 +31,9 @@ protected:
public:
/* New methods */
Scene *GetScene();
void SetMouse(int x, int y);
int GetWidth();
int GetHeight();
void SetPOV(int x, int y);

private:
MapViewerData *data;


Loading…
Cancel
Save