diff game/IDMWidget.java @ 162:e8eeac403e5f

Backed out changeset fb33d3796942
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 01 Dec 2016 14:33:25 +0200
parents src/IDMWidget.java@fb33d3796942
children b9bc493ae53c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/IDMWidget.java	Thu Dec 01 14:33:25 2016 +0200
@@ -0,0 +1,163 @@
+/*
+ * Ristipolku IDM base widget
+ * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
+ */
+package game;
+
+import java.awt.*;
+import java.awt.event.*;
+
+
+public class IDMWidget
+{
+    IDMWidget parent;
+    IDMPoint pos, size, scale;
+    int keyCode;
+
+    public IDMWidget()
+    {
+        keyCode = -1;
+        this.scale = new IDMPoint(1, 1);
+        this.pos = new IDMPoint(0, 0);
+        this.size = new IDMPoint(0, 0);
+    }
+
+    public IDMWidget(IDMPoint pos)
+    {
+        this();
+        this.pos = pos;
+    }
+
+    public IDMWidget(IDMPoint pos, IDMPoint size)
+    {
+        this();
+        this.pos = pos;
+        this.size = size;
+    }
+
+    public void setParent(IDMWidget par)
+    {
+        this.parent = par;
+    }
+
+    public void add(IDMWidget widget)
+    {
+    }
+
+    public void remove(IDMWidget widget)
+    {
+    }
+
+    public void setPos(IDMPoint pos)
+    {
+        this.pos = pos;
+    }
+
+    public void setPos(float x, float y)
+    {
+        this.pos = new IDMPoint(x, y);
+    }
+
+    public void setSize(IDMPoint size)
+    {
+        this.size = size;
+    }
+
+    public void setSize(float w, float h)
+    {
+        this.size = new IDMPoint(w, h);
+    }
+
+    public void setScale(IDMPoint scale)
+    {
+        this.scale = scale;
+    }
+
+    public void setScale(float x, float y)
+    {
+        this.setScale(new IDMPoint(x, y));
+    }
+
+    public int getScaledX()
+    {
+        return (int) (pos.x * scale.x);
+    }
+
+    public int getScaledY()
+    {
+        return (int) (pos.y * scale.y);
+    }
+
+    public int getScaledWidth()
+    {
+        return (int) (size.x * scale.x);
+    }
+
+    public int getScaledHeight()
+    {
+        return (int) (size.y * scale.y);
+    }
+
+    public boolean contains(float x, float y)
+    {
+        return (x >= getScaledX() &&
+                y >= getScaledY() &&
+                x < getScaledX() + getScaledWidth() &&
+                y < getScaledY() + getScaledHeight());
+    }
+
+    public boolean contains(Point where)
+    {
+        return contains(where.x, where.y);
+    }
+
+    public boolean contains(IDMPoint where)
+    {
+        return contains(where.x, where.y);
+    }
+
+    public void paint(Graphics2D g)
+    {
+    }
+
+    public boolean mousePressed(MouseEvent e)
+    {
+        return false;
+    }
+
+    public boolean mouseReleased(MouseEvent e)
+    {
+        if (contains(e.getPoint()))
+        {
+            clicked();
+            return true;
+        }
+        return false;
+    }
+
+    public boolean mouseEntered(MouseEvent e)
+    {
+        return false;
+    }
+
+    public boolean mouseExited(MouseEvent e)
+    {
+        return false;
+    }
+
+    // Generic key handler
+    public boolean keyPressed(KeyEvent e)
+    {
+        if (e.getKeyCode() == keyCode)
+        {
+            clicked();
+            return true;
+        }
+        else
+            return false;
+    }
+
+    public void clicked()
+    {
+    }
+}