Date:2010-09-24 07:04:04 (8 years 8 months ago)
Author:Werner Almesberger
Commit:edb12b6e956b9d85def465745c80433154afd0ef
Message:When leveling, the center circle is now shown when the pointer approaches it.

- solidify/Makefile: added util.o
- solidify/util.h, solidify/util.c (draw_circle): wrapper for gdk_draw_arc
- solidify/Makefile: added style.o
- solidify/style.h, solidify/style.c, solidify/solidify.c (main): GUI style
parameters and items
- solidify/level.c (scroll_z. scroll_xy, scroll_event, level),
solidify/overlap.c (scroll_event, overlap): renamed "da" to "darea", to
avoid confusion with "da" for GdkDrawable in style.c
- solidify/level.c (r_center, scroll_event): moved center radius
calculation to separate function
- solidify/level.c (draw_image): renamed to draw_map
- solidify/level.c (draw_image): draw an on-screen display (OSD)
- solidify/level.c (scroll_z, scroll_xy, expose_event, level): propagate
OSD on/off switch
- solidify/level.c (scroll_event, motion_notify_event, level): enable OSD
when approaching the center circle
Files: solidify/Makefile (1 diff)
solidify/level.c (4 diffs)
solidify/overlap.c (3 diffs)
solidify/solidify.c (2 diffs)
solidify/style.c (1 diff)
solidify/style.h (1 diff)
solidify/util.c (1 diff)
solidify/util.h (2 diffs)

Change Details

solidify/Makefile
1212
1313SHELL = /bin/bash
1414
15OBJS = array.o face.o histo.o level.o overlap.o solidify.o
15OBJS = array.o face.o histo.o level.o overlap.o solidify.o style.o util.o
1616
1717CFLAGS_WARN = -Wall -Wshadow -Wmissing-prototypes \
1818          -Wmissing-declarations -Wno-format-zero-length
solidify/level.c
1616#include <math.h>
1717#include <gtk/gtk.h>
1818
19#include "util.h"
1920#include "array.h"
2021#include "face.h"
22#include "style.h"
2123#include "level.h"
2224
2325
2426#define NEAR 1
2527
2628
27static void draw_image(GtkWidget *widget, struct face *f)
29static int has_osd;
30
31
32static double r_center(const struct face *f)
33{
34    return hypot(f->sx, f->sy)/LEVEL_CENTER_DIV;
35}
36
37
38static void draw_map(GtkWidget *widget, struct face *f)
2839{
2940    int x, y, z;
3041    double z0;
...... 
7081}
7182
7283
73static void scroll_z(GtkWidget *da, struct face *f, int up)
84static void draw_image(GtkWidget *widget, struct face *f, int osd)
85{
86    draw_map(widget, f);
87    has_osd = osd;
88    if (osd)
89        draw_circle(widget->window, gc_osd,
90            f->sx/2, f->sy/2, r_center(f));
91}
92
93
94static void scroll_z(GtkWidget *darea, struct face *f, int up, int osd)
7495{
7596    if (up) {
7697        if (f->z_ref < f->a->max_z)
...... 
79100        if (f->z_ref > f->a->min_z)
80101            f->z_ref--;
81102    }
82    draw_image(da, f);
103    draw_image(darea, f, osd);
83104}
84105
85106
86static void scroll_xy(GtkWidget *da, struct face *f, int dx, int dy, int up)
107static void scroll_xy(GtkWidget *darea, struct face *f, int dx, int dy, int up,
108    int osd)
87109{
88110    double d;
89111
90112    d = (double) (up ? 1 : -1)/(dx*dx+dy*dy)/2.0;
91113    f->fx += d*dx;
92114    f->fy += d*dy;
93    draw_image(da, f);
115    draw_image(darea, f, osd);
94116}
95117
96118
97119static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event,
98120    gpointer data)
99121{
100    GtkWidget *da = gtk_bin_get_child(GTK_BIN(widget));
122    GtkWidget *darea = gtk_bin_get_child(GTK_BIN(widget));
101123    struct face *f = data;
102124    int dx = event->x-f->sx/2;
103125    int dy = event->y-f->sy/2;
104    int center = hypot(dx, dy)/hypot(f->sx, f->sy) < 0.25;
126    double r = hypot(dx, dy);
127    double rc = r_center(f);
128    int center = r < rc;
129    int osd = fabs(r-rc) < OSD_PROXIMITY;
105130
106131    switch (event->direction) {
107132    case GDK_SCROLL_UP:
108133        if (center)
109            scroll_z(da, f, 0);
134            scroll_z(darea, f, 0, osd);
110135        else
111            scroll_xy(da, f, dx, dy, 1);
136            scroll_xy(darea, f, dx, dy, 1, osd);
112137        break;
113138    case GDK_SCROLL_DOWN:
114139        if (center)
115            scroll_z(da, f, 1);
140            scroll_z(darea, f, 1, osd);
116141        else
117            scroll_xy(da, f, dx, dy, 0);
142            scroll_xy(darea, f, dx, dy, 0, osd);
118143        break;
119144    default:
120145        /* ignore */;
...... 
126151static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event,
127152    gpointer user_data)
128153{
129    draw_image(widget, user_data);
154    draw_image(widget, user_data, has_osd);
130155    return TRUE;
131156}
132157
133158
159static gboolean motion_notify_event(GtkWidget *widget, GdkEventMotion *event,
160    gpointer data)
161{
162    struct face *f = data;
163    int dx = event->x-f->sx/2;
164    int dy = event->y-f->sy/2;
165    double r = hypot(dx, dy);
166    double rc = r_center(f);
167    int osd = fabs(r-rc) < OSD_PROXIMITY;
168
169    if (osd != has_osd)
170        draw_image(widget, f, osd);
171    return FALSE;
172}
173
174
134175void level(GtkWidget *canvas, struct face *f)
135176{
136    GtkWidget *evbox, *da;
177    GtkWidget *evbox, *darea;
137178
138179    evbox = gtk_event_box_new();
139    da = gtk_drawing_area_new();
140    gtk_widget_set_size_request(da, f->sx, f->sy);
180    darea = gtk_drawing_area_new();
181
182    gtk_widget_set_events(darea,
183        GDK_EXPOSE | GDK_KEY_PRESS_MASK |
184        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
185        GDK_SCROLL |
186        GDK_POINTER_MOTION_MASK);
187
188    gtk_widget_set_size_request(darea, f->sx, f->sy);
141189    gtk_container_add(GTK_CONTAINER(canvas), evbox);
142    gtk_container_add(GTK_CONTAINER(evbox), da);
190    gtk_container_add(GTK_CONTAINER(evbox), darea);
143191
144    draw_image(da, f);
192    draw_image(darea, f, 0);
145193
146194    g_signal_connect(G_OBJECT(evbox), "scroll-event",
147195        G_CALLBACK(scroll_event), f);
148    g_signal_connect(G_OBJECT(da), "expose-event",
196    g_signal_connect(G_OBJECT(darea), "expose-event",
149197        G_CALLBACK(expose_event), f);
198    g_signal_connect(G_OBJECT(darea), "motion-notify-event",
199        G_CALLBACK(motion_notify_event), f);
150200}
solidify/overlap.c
203203static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event,
204204    gpointer data)
205205{
206    GtkWidget *da = gtk_bin_get_child(GTK_BIN(widget));
206    GtkWidget *darea = gtk_bin_get_child(GTK_BIN(widget));
207207    struct solid *s = data;
208208    int dx = event->x-sx(s)/2;
209209    int dy = event->y-sy(s)/2;
...... 
218218            shift(&s->a->m, dx, dy, 1);
219219        else
220220            rotate(&s->a->m, r);
221        draw_image(da, s);
221        draw_image(darea, s);
222222        break;
223223    case GDK_SCROLL_DOWN:
224224        if (center)
225225            shift(&s->a->m, dx, dy, -1);
226226        else
227227            rotate(&s->a->m, -r);
228        draw_image(da, s);
228        draw_image(darea, s);
229229        break;
230230    default:
231231        /* ignore */;
...... 
245245
246246void overlap(GtkWidget *canvas, struct solid *s)
247247{
248    GtkWidget *evbox, *da;
248    GtkWidget *evbox, *darea;
249249
250250    evbox = gtk_event_box_new();
251    da = gtk_drawing_area_new();
252    gtk_widget_set_size_request(da, sx(s), sy(s));
251    darea = gtk_drawing_area_new();
252    gtk_widget_set_size_request(darea, sx(s), sy(s));
253253    gtk_container_add(GTK_CONTAINER(canvas), evbox);
254    gtk_container_add(GTK_CONTAINER(evbox), da);
254    gtk_container_add(GTK_CONTAINER(evbox), darea);
255255
256    draw_image(da, s);
256    draw_image(darea, s);
257257
258258    g_signal_connect(G_OBJECT(evbox), "scroll-event",
259259        G_CALLBACK(scroll_event), s);
260    g_signal_connect(G_OBJECT(da), "expose-event",
260    g_signal_connect(G_OBJECT(darea), "expose-event",
261261        G_CALLBACK(expose_event), s);
262262}
solidify/solidify.c
1818
1919#include "face.h"
2020#include "solid.h"
21#include "style.h"
2122#include "level.h"
2223#include "overlap.h"
2324
...... 
102103    level(canvas, solid.a);
103104    active = solid.a;
104105
106    init_style(root->window);
107
105108    gtk_widget_show_all(root);
106109
107110    g_signal_connect(G_OBJECT(root), "key-press-event",
solidify/style.c
1/*
2 * style.c - GUI style parameters and items
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <gtk/gtk.h>
16
17#include "style.h"
18
19
20GdkGC *gc_osd;
21
22
23static GdkColor get_color(GdkDrawable *da, const char *spec)
24{
25    GdkColormap *cmap;
26    GdkColor color;
27
28    cmap = gdk_drawable_get_colormap(da);
29    if (!gdk_color_parse(spec, &color))
30        abort();
31    if (!gdk_colormap_alloc_color(cmap, &color, FALSE, TRUE))
32        abort();
33    return color;
34}
35
36
37static GdkGC *gc(GdkDrawable *da, const char *spec, int width)
38{
39    GdkGCValues gc_values = {
40        .background = get_color(da, "black"),
41        .foreground = get_color(da, spec),
42        .line_width = width,
43    };
44
45    return gdk_gc_new_with_values(da, &gc_values,
46        GDK_GC_FOREGROUND | GDK_GC_BACKGROUND | GDK_GC_LINE_WIDTH);
47}
48
49
50void init_style(GdkDrawable *da)
51{
52    gc_osd = gc(da, "#ffff00", 4);
53}
solidify/style.h
1/*
2 * style.h - GUI style parameters and items
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef STYLE_H
14#define STYLE_H
15
16#include <gtk/gtk.h>
17
18
19extern GdkGC *gc_osd;
20
21
22#define OSD_PROXIMITY 20
23#define LEVEL_CENTER_DIV 5
24
25
26void init_style(GdkDrawable *da);
27
28#endif /* !STYLE_H */
solidify/util.c
1/*
2 * util.c - Common utility functions
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <gtk/gtk.h>
15
16#include "util.h"
17
18
19void draw_circle(GdkDrawable *da, GdkGC *gc, int x, int y, int r)
20{
21    gdk_draw_arc(da, gc, FALSE, x-r, y-r, 2*r, 2*r, 0, 360*64);
22}
solidify/util.h
1414#define UTIL_H
1515
1616#include <stdlib.h>
17#include <gtk/gtk.h>
1718
1819
1920#define alloc_size(s) \
...... 
2425
2526#define alloc_type(t) ((t *) alloc_size(sizeof(t)))
2627
28
29void draw_circle(GdkDrawable *da, GdkGC *gc, int x, int y, int r);
30
2731#endif /* UTIL_H */

Archive Download the corresponding diff file

Branches:
master



interactive