[gnome-games/glines-vala] add a very basic view based on GtkDrawingArea
- From: Thomas Hindoe Paaboel Andersen <thomashpa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games/glines-vala] add a very basic view based on GtkDrawingArea
- Date: Sun, 10 Jun 2012 21:49:18 +0000 (UTC)
commit 86560aee0175a9b1b8dc11f9eb8230f0bbcff9fe
Author: Thomas Hindoe Paaboel Andersen <phomes gmail com>
Date: Sun Jun 10 23:49:08 2012 +0200
add a very basic view based on GtkDrawingArea
glines/src/Makefile.am | 1 +
glines/src/glines-application.vala | 11 ++-
glines/src/glines-view-2d.vala | 143 ++++++++++++++++++++++++++++++++++++
glines/src/glines-view-cli.vala | 5 +-
4 files changed, 156 insertions(+), 4 deletions(-)
---
diff --git a/glines/src/Makefile.am b/glines/src/Makefile.am
index 47aec5f..423fbc8 100644
--- a/glines/src/Makefile.am
+++ b/glines/src/Makefile.am
@@ -10,6 +10,7 @@ glines_SOURCES = \
glines-field.vala \
glines-piece.vala \
glines-preview-queue.vala \
+ glines-view-2d.vala \
glines-view-cli.vala \
$(BUILT_SOURCES)
diff --git a/glines/src/glines-application.vala b/glines/src/glines-application.vala
index cb28587..5b1444f 100644
--- a/glines/src/glines-application.vala
+++ b/glines/src/glines-application.vala
@@ -29,6 +29,8 @@ namespace Glines
private const string[] authors = { "Thomas Andersen <phomes gmail com>", "Robert Szokovacs <szo appaloosacorp hu>", "Szabolcs B\xc3\xa1n <shooby gnome hu>" };
//private const string[] documenters = { "Tiffany Antopolski", "Lanka Rathnayaka" };
+ private GlinesBoard board = new GlinesBoard(10, 10, 5, 3);
+
public GlinesApp ()
{
Object (application_id: "org.gnome.glines", flags: ApplicationFlags.FLAGS_NONE);
@@ -73,14 +75,19 @@ namespace Glines
section.append (_("_Quit"), "app.quit");
set_app_menu (menu);
+ var box = (Gtk.Box) builder.get_object ("vbox");
+ var view2d = new View2D (board);
+ box.add (view2d);
+ view2d.show ();
+
window = (Gtk.ApplicationWindow) builder.get_object ("glines_window");
add_window (window);
}
public override void activate ()
{
- var v = new ViewCli ();
- v.run();
+ //var v = new ViewCli (board);
+ //v.run();
window.present ();
}
diff --git a/glines/src/glines-view-2d.vala b/glines/src/glines-view-2d.vala
new file mode 100644
index 0000000..82c913f
--- /dev/null
+++ b/glines/src/glines-view-2d.vala
@@ -0,0 +1,143 @@
+using Cairo;
+using Gdk;
+using Gtk;
+
+namespace Glines
+{
+ public class View2D : DrawingArea
+ {
+ public GlinesBoard board { get; private set; }
+ public double line_width { get; set; }
+
+ public View2D(GlinesBoard board)
+ {
+ this.board = board;
+ this.line_width = 1.0;
+
+ this.vexpand = true;
+ this.hexpand = true;
+ this.add_events (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | Gdk.EventMask.KEY_PRESS_MASK);
+
+ this.board.changed.connect ((o) => this.queue_draw());
+ //TODO: drop this later:
+ this.board.info.connect ((o, info) => stdout.printf(info + "\n"));
+ }
+
+
+ public override bool draw(Context ctx)
+ {
+ int w = this.get_allocated_width();
+ int h = this.get_allocated_height();
+
+ double boxsize_h = (w - (1 + board.rows) * line_width) / board.rows;
+ double boxsize_v = (h - (1 + board.cols) * line_width) / board.cols;
+
+ //Draw the backgrounds first
+ for (int i = 0; i < board.cols; i++)
+ for (int j = 0; j < board.rows; j++)
+ this.draw_field(this, ctx, j, i);
+
+ ctx.set_source_rgb(52, 95, 108);
+ ctx.set_line_width(line_width);
+
+ for (int i = 0; i < board.rows; i++)
+ {
+ ctx.move_to(0.5 + i * (boxsize_h + line_width), 0.5);
+ ctx.line_to(0.5 + i * (boxsize_h + line_width), 0.5 + h);
+ }
+
+ for (int i = 0; i < board.cols; i++)
+ {
+ ctx.move_to(0.5 + 0, 0.5 + i * (boxsize_v + line_width));
+ ctx.line_to(0.5 + w, 0.5 + i * (boxsize_v + line_width));
+ }
+
+ ctx.rectangle(0.5, 0.5, w - 0.5, h - 0.5);
+ ctx.stroke();
+
+ /* Cursor */
+ if (board.show_cursor)
+ {
+ ctx.set_source_rgb(255, 0, 0);
+ ctx.set_line_width(line_width);
+
+ ctx.rectangle(board.cursor_x * (boxsize_h + line_width) + 1.5, board.cursor_y * (boxsize_v + line_width) + 1.5, (boxsize_h + line_width) - 2.5, (boxsize_v + line_width) - 2.5);
+ ctx.stroke();
+ }
+
+ return true;
+ }
+
+ private void draw_field(Widget da, Context ctx, int x, int y)
+ {
+ int w = da.get_allocated_width();
+ int h = da.get_allocated_height();
+
+ double boxsizeH = (w - (1 + board.rows) * line_width) / board.rows;
+ double boxsizeV = (h - (1 + board.cols) * line_width) / board.cols;
+
+ var field = board.fields[x, y];
+
+ ctx.set_source_rgb(50, 50, 50);
+
+ ctx.rectangle(x * (boxsizeH + line_width) + 1.5, y * (boxsizeV + line_width) + 1.5, (boxsizeH + line_width) - 2.5, (boxsizeV + line_width) - 2.5);
+ ctx.fill();
+
+ if (field.piece != null)
+ {
+ this.draw_piece(da, ctx, field.piece, x, y);
+ }
+ }
+
+ private void draw_piece(Widget da, Context ctx, GlinesPiece p, int x, int y)
+ {
+ int w = da.get_allocated_width();
+ int h = da.get_allocated_height();
+
+ double boxsizeH = (w - (1 + board.rows) * line_width) / board.rows;
+ double boxsizeV = (h - (1 + board.cols) * line_width) / board.cols;
+
+ //TODO: look up surfaces instead. This is just for debug:
+ if (p.id == 0)
+ ctx.set_source_rgb(255, 0, 0);
+ else if (p.id == 1)
+ ctx.set_source_rgb(0, 255, 0);
+ else if (p.id == 2)
+ ctx.set_source_rgb(0, 0, 255);
+ else
+ ctx.set_source_rgb(0, 0, 0);
+
+
+ ctx.rectangle(x * (boxsizeH + line_width) + 1.5, y * (boxsizeV + line_width) + 1.5, (boxsizeH + line_width) - 2.5, (boxsizeV + line_width) - 2.5);
+ ctx.fill();
+ }
+
+ public override bool key_press_event (EventKey e)
+ {
+ var key = e.keyval;
+
+ if (key == Gdk.Key.Up) this.board.cursor_y--;
+ if (key == Gdk.Key.Down) this.board.cursor_y++;
+ if (key == Gdk.Key.Left) this.board.cursor_x--;
+ if (key == Gdk.Key.Right) this.board.cursor_x++;
+
+ if (key == Gdk.Key.space) board.select_field(board.cursor_x, board.cursor_y);
+
+ return true;
+ }
+
+ public override bool button_press_event (EventButton e)
+ {
+ double boxsize_x = this.get_allocated_width() / (board.rows * 1.0);
+ int x = (int)(e.x / boxsize_x);
+
+ double boxsize_y = this.get_allocated_height() / (board.cols * 1.0);
+ int y = (int)(e.y / boxsize_y);
+
+ board.place_cursor(x, y);
+ board.select_field(x, y);
+
+ return false;
+ }
+ }
+}
diff --git a/glines/src/glines-view-cli.vala b/glines/src/glines-view-cli.vala
index 96df9cf..42a7c6c 100644
--- a/glines/src/glines-view-cli.vala
+++ b/glines/src/glines-view-cli.vala
@@ -4,14 +4,15 @@ namespace Glines
{
class ViewCli
{
- private GlinesBoard board = new GlinesBoard(10, 10, 5, 3);
+ private GlinesBoard board;
private bool gameover = false;
private string message = "";
private char[] id_to_char_symbol = { 'a', 'b', 'c', 'd', 'e' };
- public ViewCli()
+ public ViewCli(GlinesBoard board)
{
+ this.board = board;
board.show_cursor = true;
board.place_cursor(5,5);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]