[longomatch/gameunits] Wip
- From: Andoni Morales Alastruey <amorales src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [longomatch/gameunits] Wip
- Date: Fri, 9 Dec 2011 00:20:46 +0000 (UTC)
commit 89b01881593fe65a8097368b879958912c8b7434
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date: Fri Dec 9 01:20:27 2011 +0100
Wip
LongoMatch.Core/Common/Image.cs | 84 +++++++++++++++++++++++++++++++++++++++
LongoMatch.GUI/Gui/Cairo.cs | 83 ++++++++++++++++++++++++++++++++++++++
LongoMatch.GUI/Gui/Helpers.cs | 84 +++++++++++++++++++++++++++++++++++++++
3 files changed, 251 insertions(+), 0 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Image.cs b/LongoMatch.Core/Common/Image.cs
new file mode 100644
index 0000000..61ecaca
--- /dev/null
+++ b/LongoMatch.Core/Common/Image.cs
@@ -0,0 +1,84 @@
+//
+// Copyright (C) 2011 Andoni Morales Alastruey
+//
+// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+
+namespace LongoMatch.Common
+{
+ using System;
+ using System.IO;
+#if HAVE_GTK
+ using SImage = Gdk.Pixbuf;
+#else
+ using System.Drawing.Imaging;
+ using SImage = System.Drawing.Image;
+#endif
+
+ public class Image
+ {
+ SImage image;
+
+ public Image (SImage image)
+ {
+ this.image = image;
+ }
+
+ public SImage Value {
+ get {
+ return image;
+ }
+ }
+
+#if HAVE_GTK
+ public byte[] Serialize () {
+ byte[] ser;
+
+ if (image == null)
+ return null;
+ return image.SaveToBuffer("png");
+ }
+
+ static Pixbuf Deserialize (byte[] ser) {
+ return new Pixbuf(ser);
+ }
+#else
+ public byte[] Serialize () {
+ if (image == null)
+ return null;
+ using (MemoryStream stream = new MemoryStream()) {
+ image.Save(stream, ImageFormat.Png);
+ byte[] buf = new byte[stream.Length - 1];
+ stream.Position = 0;
+ stream.Read(buf, 0, buf.Length);
+ return buf;
+ }
+ }
+
+ public static Image Deserialize (byte[] ser) {
+ Image img = null;
+ using (MemoryStream stream = new MemoryStream(ser)) {
+ img = new Image(System.Drawing.Image.FromStream(stream));
+ }
+ return img;
+ }
+
+ public void Dispose() {
+ image.Dispose();
+ }
+#endif
+ }
+}
+
diff --git a/LongoMatch.GUI/Gui/Cairo.cs b/LongoMatch.GUI/Gui/Cairo.cs
new file mode 100644
index 0000000..c4247e3
--- /dev/null
+++ b/LongoMatch.GUI/Gui/Cairo.cs
@@ -0,0 +1,83 @@
+//
+// Copyright (C) 2011 Andoni Morales Alastruey
+//
+// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+using Cairo;
+
+
+namespace LongoMatch.Common
+{
+ public class CairoUtils
+ {
+ Gdk.ModifierType
+ public static void DrawRoundedRectangle(Cairo.Context gr, double x, double y,
+ double width, double height, double radius,
+ Cairo.Color color, Cairo.Color borderColor)
+ {
+ gr.Save();
+
+ if((radius > height / 2) || (radius > width / 2))
+ radius = Math.Min(height / 2, width / 2);
+
+ gr.MoveTo(x, y + radius);
+ gr.Arc(x + radius, y + radius, radius, Math.PI, -Math.PI / 2);
+ gr.LineTo(x + width - radius, y);
+ gr.Arc(x + width - radius, y + radius, radius, -Math.PI / 2, 0);
+ gr.LineTo(x + width, y + height - radius);
+ gr.Arc(x + width - radius, y + height - radius, radius, 0, Math.PI / 2);
+ gr.LineTo(x + radius, y + height);
+ gr.Arc(x + radius, y + height - radius, radius, Math.PI / 2, Math.PI);
+ gr.ClosePath();
+ gr.Restore();
+
+ gr.LineJoin = LineJoin.Round;
+ gr.Color = borderColor;
+ gr.StrokePreserve();
+ gr.Color = color;
+ gr.Fill();
+ }
+
+ public static void DrawLine(Cairo.Context g, double x1, double y1,
+ double x2, double y2,
+ int width, Cairo.Color color) {
+ g.Color = color;
+ g.Operator = Operator.Over;
+ g.LineWidth = width;
+ g.MoveTo(x1, y1);
+ g.LineTo(x2,y2);
+ g.Stroke();
+ }
+
+ public static void DrawTriangle(Cairo.Context g, double x, double y,
+ int width, int height, Cairo.Color color) {
+ g.Color = color;
+ g.MoveTo(x, y);
+ g.LineTo(x + width/2, y-height);
+ g.LineTo(x - width/2, y-height);
+ g.ClosePath();
+ g.Fill();
+ g.Stroke();
+ }
+
+ public static Cairo.Color RGBToCairoColor(Gdk.Color gdkColor) {
+ return new Cairo.Color((double)(gdkColor.Red)/ushort.MaxValue,
+ (double)(gdkColor.Green)/ushort.MaxValue,
+ (double)(gdkColor.Blue)/ushort.MaxValue);
+ }
+ }
+}
+
diff --git a/LongoMatch.GUI/Gui/Helpers.cs b/LongoMatch.GUI/Gui/Helpers.cs
new file mode 100644
index 0000000..c9d5a57
--- /dev/null
+++ b/LongoMatch.GUI/Gui/Helpers.cs
@@ -0,0 +1,84 @@
+//
+// Copyright (C) 2011 Andoni Morales Alastruey
+//
+// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+using System.IO;
+using Gtk;
+using Gdk;
+using Mono.Unix;
+
+namespace LongoMatch.Gui
+{
+ public class Helpers
+ {
+ public static FileFilter GetFileFilter() {
+ FileFilter filter = new FileFilter();
+ filter.Name = "Images";
+ filter.AddPattern("*.png");
+ filter.AddPattern("*.jpg");
+ filter.AddPattern("*.jpeg");
+ return filter;
+ }
+
+ public static Pixbuf OpenImage(Gtk.Window toplevel) {
+ Pixbuf pimage = null;
+ StreamReader file;
+ FileChooserDialog fChooser;
+
+ fChooser = new FileChooserDialog(Catalog.GetString("Choose an image"),
+ toplevel, FileChooserAction.Open,
+ "gtk-cancel",ResponseType.Cancel,
+ "gtk-open",ResponseType.Accept);
+ fChooser.AddFilter(GetFileFilter());
+ if(fChooser.Run() == (int)ResponseType.Accept) {
+ // For Win32 compatibility we need to open the image file
+ // using a StreamReader. Gdk.Pixbuf(string filePath) uses GLib to open the
+ // input file and doesn't support Win32 files path encoding
+ file = new StreamReader(fChooser.Filename);
+ pimage= new Gdk.Pixbuf(file.BaseStream);
+ file.Close();
+ }
+ fChooser.Destroy();
+ return pimage;
+ }
+
+ public static Pixbuf Scale(Pixbuf pixbuf, int max_width, int max_height) {
+ int ow,oh,h,w;
+
+ h = ow = pixbuf.Height;
+ w = oh = pixbuf.Width;
+ ow = max_width;
+ oh = max_height;
+
+ if(w>max_width || h>max_height) {
+ Pixbuf scalledPixbuf;
+ double rate = (double)w/(double)h;
+
+ if(h>w)
+ ow = (int)(oh * rate);
+ else
+ oh = (int)(ow / rate);
+ scalledPixbuf = pixbuf.ScaleSimple(ow,oh,Gdk.InterpType.Bilinear);
+ pixbuf.Dispose();
+ return scalledPixbuf;
+ } else {
+ return pixbuf;
+ }
+ }
+ }
+}
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]