Commit c5af43bb authored by Bruno Coudoin's avatar Bruno Coudoin

Improved crane drawing with new arrow and a rope.

	Improved electric. It's now playable in free mode with a switch,
	resistor, rheostat and bulb. Need more work to make it playable.
parent 36f57d83
2006-01-04 Bruno coudoin <bruno.coudoin@free.fr>
Improved crane drawing with new arrow and a rope.
Improved electric. It's now playable in free mode with a switch,
resistor, rheostat and bulb. Need more work to make it playable.
* boards/crane/arrow_b.png:
* boards/crane/arrow_l.png:
* boards/crane/arrow_r.png:
* boards/crane/arrow_u.png:
* boards/crane/crane-bg.png:
* boards/electric/bulb.png:
* boards/electric/bulb1.png:
* boards/electric/bulb10.png:
* boards/electric/bulb11.png:
* boards/electric/bulb12.png:
* boards/electric/bulb2.png:
* boards/electric/bulb3.png:
* boards/electric/bulb4.png:
* boards/electric/bulb5.png:
* boards/electric/bulb6.png:
* boards/electric/bulb7.png:
* boards/electric/bulb8.png:
* boards/electric/bulb9.png:
* boards/electric/resistor_track.png:
* boards/electric/resistor_wiper.png:
* boards/electric/switch_click.png:
* boards/electric/switch_off.png:
* boards/electric/switch_on.png:
* boards/electric/variable_resistor.svg:
* src/boards/crane.c: (start_board), (arrow_event), (draw_arrow),
(draw_redhands), (smooth_move), (select_item):
* src/boards/python/electric.py:
2006-01-02 Bruno coudoin <bruno.coudoin@free.fr>
Some code cleanup in crane.c, removed image memory leaks
......
boards/crane/arrow_b.png

331 Bytes | W: | H:

boards/crane/arrow_b.png

874 Bytes | W: | H:

boards/crane/arrow_b.png
boards/crane/arrow_b.png
boards/crane/arrow_b.png
boards/crane/arrow_b.png
  • 2-up
  • Swipe
  • Onion skin
boards/crane/arrow_l.png

362 Bytes | W: | H:

boards/crane/arrow_l.png

901 Bytes | W: | H:

boards/crane/arrow_l.png
boards/crane/arrow_l.png
boards/crane/arrow_l.png
boards/crane/arrow_l.png
  • 2-up
  • Swipe
  • Onion skin
boards/crane/arrow_r.png

361 Bytes | W: | H:

boards/crane/arrow_r.png

904 Bytes | W: | H:

boards/crane/arrow_r.png
boards/crane/arrow_r.png
boards/crane/arrow_r.png
boards/crane/arrow_r.png
  • 2-up
  • Swipe
  • Onion skin
boards/crane/arrow_u.png

330 Bytes | W: | H:

boards/crane/arrow_u.png

799 Bytes | W: | H:

boards/crane/arrow_u.png
boards/crane/arrow_u.png
boards/crane/arrow_u.png
boards/crane/arrow_u.png
  • 2-up
  • Swipe
  • Onion skin
boards/crane/crane-bg.png

37.8 KB | W: | H:

boards/crane/crane-bg.png

36.9 KB | W: | H:

boards/crane/crane-bg.png
boards/crane/crane-bg.png
boards/crane/crane-bg.png
boards/crane/crane-bg.png
  • 2-up
  • Swipe
  • Onion skin
This diff is collapsed.
......@@ -28,18 +28,17 @@
// Define
#define SOUNDLISTFILE PACKAGE
#define CRANE_BUTTON_WIDTH 20
#define CRANE_BUTTON_HEIGHT 50
#define CRANE_BUTTON_SPACE 20 // Space between a pair (left/right or up/down)
#define CRANE_BUTTON_LEFT_X 200
#define CRANE_BUTTON_LEFT_Y 87
#define CRANE_BUTTON_RIGHT_X (CRANE_BUTTON_LEFT_X + CRANE_BUTTON_HEIGHT + CRANE_BUTTON_SPACE)
#define CRANE_BUTTON_RIGHT_Y CRANE_BUTTON_LEFT_Y
#define CRANE_BUTTON_UP_X 374
#define CRANE_BUTTON_UP_Y 185
#define CRANE_BUTTON_DOWN_X CRANE_BUTTON_UP_X
#define CRANE_BUTTON_DOWN_Y (CRANE_BUTTON_UP_Y + CRANE_BUTTON_HEIGHT + CRANE_BUTTON_SPACE)
#define CRANE_BUTTON_SPACE 40 // Space between a pair (left/right or up/down)
#define CRANE_BUTTON_LEFT_X 217
#define CRANE_BUTTON_LEFT_Y 77
#define CRANE_BUTTON_RIGHT_X (CRANE_BUTTON_LEFT_X + CRANE_BUTTON_SPACE)
#define CRANE_BUTTON_RIGHT_Y CRANE_BUTTON_LEFT_Y + 2
#define CRANE_BUTTON_UP_X 372
#define CRANE_BUTTON_UP_Y 215
#define CRANE_BUTTON_DOWN_X CRANE_BUTTON_UP_X + 1
#define CRANE_BUTTON_DOWN_Y (CRANE_BUTTON_UP_Y + CRANE_BUTTON_SPACE)
#define CRANE_ROPE_Y CRANE_BUTTON_LEFT_Y + 27
#define CRANE_FRAME_X 38
#define CRANE_FRAME_Y 168
#define CRANE_FRAME_COLUMN 6
......@@ -51,10 +50,11 @@
#define CRANE_FRAME_MODEL_X 460
#define CRANE_FRAME_MODEL_Y 107
#define RIGHT 0
#define LEFT 1
#define UP 2
#define DOWN 3
#define DOWN 0 /* Warning ordering is important */
#define UP 1
#define LEFT 2
#define RIGHT 3
#define MAX_LEVEL 6 // Don't raise this number except if putting more values in pixmap[] array, in place_item function
#define NB_ELEMENT (2 * MAX_LEVEL) + 2
......@@ -83,12 +83,14 @@ static gboolean board_paused = TRUE;
static GnomeCanvasGroup *boardRootItem = NULL;
static GnomeCanvasItem *selected_item = NULL;
static GnomeCanvasItem *red_hands = NULL;
static GnomeCanvasItem *crane_rope_item = NULL;
static gint timer_id = 0;
static gint nb_move = 0;
static gboolean moving = FALSE;
static move_object my_move;
static int list_answer[CRANE_FRAME_LINE * CRANE_FRAME_COLUMN];
static int list_game[CRANE_FRAME_LINE * CRANE_FRAME_COLUMN];
static GnomeCanvasPoints *crane_rope;
// gcompris functions
static void start_board (GcomprisBoard *agcomprisBoard);
......@@ -181,6 +183,7 @@ static void start_board (GcomprisBoard *agcomprisBoard)
gcompris_set_background(gnome_canvas_root(gcomprisBoard->canvas),
gcompris_image_to_skin("gcompris-bg.jpg"));
crane_next_level();
gamewon = FALSE;
......@@ -366,7 +369,7 @@ static gint arrow_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data) {
int success;
double x,y;
double dx1, dy1, dx2, dy2;
int direction;
int direction = GPOINTER_TO_INT(data);
int i;
int index, new_index;
......@@ -392,25 +395,23 @@ static gint arrow_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data) {
gnome_canvas_item_get_bounds(selected_item, &dx1, &dy1, &dx2, &dy2);
if (x < CRANE_BUTTON_RIGHT_X) {
direction = LEFT;
my_move.x = -1;
my_move.y = 0;
} else if (x < (CRANE_BUTTON_RIGHT_X + CRANE_BUTTON_HEIGHT)) {
direction = RIGHT;
my_move.x = 1;
my_move.y = 0;
} else if (y < CRANE_BUTTON_DOWN_Y) {
direction = UP;
my_move.x = 0;
my_move.y = -1;
} else if (y < (CRANE_BUTTON_DOWN_Y + CRANE_BUTTON_HEIGHT)) {
direction= DOWN;
my_move.x = 0;
my_move.y = 1;
} else {
printf("Clic location error : %g %g\n", x, y);
return FALSE;
switch (direction) {
case LEFT:
my_move.x = -1;
my_move.y = 0;
break;
case RIGHT:
my_move.x = 1;
my_move.y = 0;
break;
case UP:
my_move.x = 0;
my_move.y = -1;
break;
case DOWN:
my_move.x = 0;
my_move.y = 1;
break;
}
// Check if the move doesn't go out of the frame
......@@ -456,26 +457,18 @@ static void draw_arrow() {
arrow[0].pixmap = gcompris_load_pixmap("crane/arrow_b.png");
arrow[0].x = CRANE_BUTTON_DOWN_X;
arrow[0].y = CRANE_BUTTON_DOWN_Y;
arrow[0].w = CRANE_BUTTON_WIDTH;
arrow[0].h = CRANE_BUTTON_HEIGHT;
arrow[1].pixmap = gcompris_load_pixmap("crane/arrow_u.png");
arrow[1].x = CRANE_BUTTON_UP_X;
arrow[1].y = CRANE_BUTTON_UP_Y;
arrow[1].w = CRANE_BUTTON_WIDTH;
arrow[1].h = CRANE_BUTTON_HEIGHT;
arrow[2].pixmap = gcompris_load_pixmap("crane/arrow_l.png");
arrow[2].x = CRANE_BUTTON_LEFT_X;
arrow[2].y = CRANE_BUTTON_LEFT_Y + 2;
arrow[2].w = CRANE_BUTTON_HEIGHT;
arrow[2].h = CRANE_BUTTON_WIDTH;
arrow[3].pixmap = gcompris_load_pixmap("crane/arrow_r.png");
arrow[3].x = CRANE_BUTTON_RIGHT_X;
arrow[3].y = CRANE_BUTTON_RIGHT_Y - 2;
arrow[3].w = CRANE_BUTTON_HEIGHT;
arrow[3].h = CRANE_BUTTON_WIDTH;
for (i = 0 ; i < 4 ; i++) {
item_arrow = gnome_canvas_item_new (boardRootItem,
......@@ -483,13 +476,10 @@ static void draw_arrow() {
"pixbuf", arrow[i].pixmap,
"x", arrow[i].x,
"y", arrow[i].y,
"width", arrow[i].w,
"height", arrow[i].h,
"width_set", TRUE,
"height_set", TRUE,
"anchor", GTK_ANCHOR_NW,
NULL);
gtk_signal_connect(GTK_OBJECT(item_arrow), "event", (GtkSignalFunc) arrow_event, NULL);
gtk_signal_connect(GTK_OBJECT(item_arrow), "event",
(GtkSignalFunc) arrow_event, GINT_TO_POINTER(i));
gtk_signal_connect(GTK_OBJECT(item_arrow), "event",
(GtkSignalFunc) gcompris_item_event_focus,
NULL);
......@@ -502,12 +492,27 @@ static void draw_arrow() {
// Draw the red hands object which highlight the selected object
static void draw_redhands() {
GnomeCanvasItem *item_redhands = NULL;
GdkPixbuf *pixmap;
/* Initialize the rope */
crane_rope = gnome_canvas_points_new(2);
crane_rope->coords[0] = 5;
crane_rope->coords[1] = CRANE_BUTTON_LEFT_Y;
crane_rope->coords[2] = 5;
crane_rope->coords[3] = CRANE_BUTTON_LEFT_Y;
crane_rope_item = gnome_canvas_item_new (boardRootItem,
gnome_canvas_line_get_type(),
"points", crane_rope,
"fill_color", "darkblue",
"width_units", (double) 1,
"width_pixels", (guint) 7,
NULL);
pixmap = gcompris_load_pixmap("crane/selected.png");
item_redhands = gnome_canvas_item_new (boardRootItem,
red_hands = gnome_canvas_item_new (boardRootItem,
gnome_canvas_pixbuf_get_type(),
"pixbuf", pixmap,
"x", (double) 5,
......@@ -521,9 +526,8 @@ static void draw_redhands() {
gdk_pixbuf_unref(pixmap);
gnome_canvas_item_hide(item_redhands);
gnome_canvas_item_hide(red_hands);
red_hands = item_redhands;
}
// Draw the drak frame (horizontal and vertical lines) that helps positionning elements
......@@ -625,12 +629,23 @@ static void place_item(int x, int y, int active) {
}
static guint smooth_move(move_object *move) {
double dx1, dy1, dx2, dy2;
if (nb_move == 0) {
moving = TRUE;
nb_move = move->nb;
}
gnome_canvas_item_get_bounds(red_hands, &dx1, &dy1, &dx2, &dy2);
crane_rope->coords[0] = (dx1 + dx2) / 2;
crane_rope->coords[1] = CRANE_ROPE_Y;
crane_rope->coords[2] = (dx1 + dx2) / 2;
crane_rope->coords[3] = (dy1 + dy2) / 2;
gnome_canvas_item_set (crane_rope_item,
"points", crane_rope,
NULL);
gnome_canvas_item_move(selected_item, move->x, move->y);
gnome_canvas_item_move(red_hands, move->x, move->y);
nb_move--;
......@@ -697,6 +712,15 @@ static void select_item(GnomeCanvasItem *item, int sound) {
gnome_canvas_item_show(red_hands);
crane_rope->coords[0] = (dx1 + dx2) / 2;
crane_rope->coords[1] = CRANE_ROPE_Y;
crane_rope->coords[2] = (dx1 + dx2) / 2;
crane_rope->coords[3] = (dy1 + dy2) / 2;
gnome_canvas_item_set (crane_rope_item,
"points", crane_rope,
NULL);
if (sound) gcompris_play_ogg ("gobble", NULL);
selected_item = item;
......
......@@ -243,9 +243,9 @@ class Gcompris_electric:
# Bulb
self.components.append(Bulb(self.gcomprisBoard.canvas, self.rootitem,
400, 200))
400, 200, 0.4))
self.components.append(Bulb(self.gcomprisBoard.canvas, self.rootitem,
540, 200))
540, 200, 0.2))
# Resistor
......@@ -256,10 +256,6 @@ class Gcompris_electric:
self.components.append(Switch(self.gcomprisBoard.canvas, self.rootitem,
350, 400))
# Rheostat
self.components.append(Rheostat(self.gcomprisBoard.canvas, self.rootitem,
700, 200))
# Battery
self.components.append(Battery(self.gcomprisBoard.canvas, self.rootitem,
100, 200))
......@@ -267,6 +263,10 @@ class Gcompris_electric:
self.components.append(Battery(self.gcomprisBoard.canvas, self.rootitem,
160, 200))
# Rheostat
self.components.append(Rheostat(self.gcomprisBoard.canvas, self.rootitem,
700, 200, 2))
# ----------------------------------------------------------------------
......@@ -576,13 +576,11 @@ class Component(object):
# Return the gnucap definition for this component
def to_gnucap(self):
gnucap = self.gnucap_name
node_count = 0
for node in self.nodes:
gnucap += " "
if(node.get_wires()):
gnucap += str(node.get_wires()[0].get_wire_id())
node_count += 1
else:
# UnConnected nodes are always connected to dummy wire 0
gnucap += str(0)
......@@ -653,6 +651,10 @@ class Component(object):
# ----------------------
#
# ----------------------------------------
# RESISTOR
#
#
class Resistor(Component):
def __init__(self, canvas, rootitem,
x, y):
......@@ -667,6 +669,10 @@ class Resistor(Component):
self.show()
# ----------------------------------------
# SWITCH
#
#
class Switch(Component):
def __init__(self, canvas, rootitem,
x, y):
......@@ -716,46 +722,137 @@ class Switch(Component):
x = self.x + self.click_ofset_x,
y = self.y + self.click_ofset_y)
# ----------------------------------------
# RHEOSTAT
#
#
class Rheostat(Component):
def __init__(self, canvas, rootitem,
x, y):
x, y, resitance_k):
self.wiper_ofset_x = 1
self.wiper_ofset_min_y = 22
self.wiper_ofset_max_y = 122
self.wiper_ofset_y = self.wiper_ofset_min_y
self.resitance = resitance_k * 1000
super(Rheostat, self).__init__(canvas,
rootitem,
"R",
"10k",
self.resitance,
"electric/resistor_track.png",
[Node("electric/connect.png", "A", 5, -20),
Node("electric/connect.png", "B", 70, 70),
Node("electric/connect.png", "C", 5, 160)])
pixmap = gcompris.utils.load_pixmap("electric/resistor_wiper.png")
self.move(x, y)
self.wiper_item = self.comp_rootitem.add(
gnome.canvas.CanvasPixbuf,
pixbuf = pixmap,
x = x,
y = y,
x = self.x + self.wiper_ofset_x,
y = self.y + self.wiper_ofset_y,
)
self.move(x, y)
self.wiper_item.connect("event", self.component_click)
self.show()
# Callback event on the wiper
def component_click(self, widget, event):
if event.type == gtk.gdk.MOTION_NOTIFY:
if event.state & gtk.gdk.BUTTON1_MASK:
if(event.y>self.y+self.wiper_ofset_max_y):
self.wiper_ofset_y = self.wiper_ofset_max_y
elif(event.y<self.y+self.wiper_ofset_min_y):
self.wiper_ofset_y = self.wiper_ofset_min_y
else:
self.wiper_ofset_y = event.y - self.y
self.wiper_item.set(
y = self.y + self.wiper_ofset_y,
)
return False
# Callback event to move the component
def component_move(self, widget, event, component):
super(Rheostat, self).component_move(widget, event, component)
self.wiper_item.set(
x = self.x + self.wiper_ofset_x,
y = self.y + self.wiper_ofset_y)
# Return the gnucap definition for a single resitor of the rheostat
# node_id1 and node_id2 are the index in the list of nodes
def to_gnucap_res(self, gnucap_name, node_id1, node_id2, gnucap_value):
gnucap = gnucap_name
gnucap += " "
for i in (node_id1, node_id2):
node = self.nodes[i]
if node.get_wires():
gnucap += str(node.get_wires()[0].get_wire_id())
else:
# UnConnected nodes are always connected to dummy wire 0
gnucap += str(0)
gnucap += " "
gnucap += " "
gnucap += str(gnucap_value)
gnucap += "\n"
gnucap += ".print dc + v(%s) i(%s)\n" %(gnucap_name, gnucap_name)
return gnucap
# Return the gnucap definition for this component
# The rheostat is special, it has 3 nodes and creates
# 3 resistors for gnucap
def to_gnucap(self):
# Main resistor
gnucap = self.to_gnucap_res(self.gnucap_name + "_1", 0, 2, self.resitance)
# Top resitor
gnucap_value = self.resitance * \
(self.wiper_ofset_y - self.wiper_ofset_min_y) / \
(self.wiper_ofset_max_y - self.wiper_ofset_min_y)
gnucap += self.to_gnucap_res(self.gnucap_name + "_2", 0, 1, gnucap_value)
# Bottom resistor
gnucap += self.to_gnucap_res(self.gnucap_name + "_3", 1, 2,
self.resitance - gnucap_value)
return gnucap
# ----------------------------------------
# BULB
#
#
class Bulb(Component):
def __init__(self, canvas, rootitem,
x, y):
x, y, power_max):
super(Bulb, self).__init__(canvas,
rootitem,
"R",
"1k",
"electric/bulb.png",
[Node("electric/connect.png", "A", -15, 150),
Node("electric/connect.png", "B", 85, 150)])
"electric/bulb1.png",
[Node("electric/connect.png", "A", -15, 215),
Node("electric/connect.png", "B", 85, 215)])
self.move(x, y)
self.show()
self.power_max = power_max
# Change the pixmap depending on the real power in the Bulb
def set_voltage_intensity(self, voltage, intensity):
super(Bulb, self).set_voltage_intensity(voltage, intensity)
power = abs(voltage * intensity)
image_index = min((power * 10) / self.power_max + 1, 11)
pixmap = gcompris.utils.load_pixmap("electric/bulb%d.png" %(image_index,))
print "Power = %f (Max=%f) Image index = %d" %(power, self.power_max, image_index)
self.component_item.set(pixbuf = pixmap)
# ----------------------------------------
# BATTERY
#
#
class Battery(Component):
def __init__(self, canvas, rootitem,
x, y):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment