Add Drawing App

This commit is contained in:
daleclack 2021-07-22 08:51:49 +08:00
parent a14c96f4ea
commit d35779f2ee
6 changed files with 204 additions and 21 deletions

View File

@ -47,11 +47,11 @@ if(WIN32)
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/icon.ico OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/icon.ico
) )
add_executable(My_GtkUi WIN32 ${app_WINRC} src/main.cpp src/background.cpp src/game.cpp add_executable(My_GtkUi WIN32 ${app_WINRC} src/main.cpp src/background.cpp src/game.cpp
src/TextEditor.cpp src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp src/TextEditor.cpp src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp src/drawing.cpp
${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}) ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
else() else()
add_executable(My_GtkUi src/main.cpp src/background.cpp src/game.cpp src/TextEditor.cpp add_executable(My_GtkUi src/main.cpp src/background.cpp src/game.cpp src/TextEditor.cpp
src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp src/drawing.cpp
${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}) ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
endif(WIN32) endif(WIN32)

View File

@ -10,11 +10,6 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">False</property> <property name="can-focus">False</property>
</object> </object>
<object class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-about</property>
</object>
<object class="GtkPopover" id="popover1"> <object class="GtkPopover" id="popover1">
<property name="width-request">300</property> <property name="width-request">300</property>
<property name="height-request">400</property> <property name="height-request">400</property>
@ -188,6 +183,20 @@
<property name="position">1</property> <property name="position">1</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkButton" id="btndraw">
<property name="label" translatable="yes">Drawing App</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="relief">none</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child> <child>
<object class="GtkButton" id="btnabout"> <object class="GtkButton" id="btnabout">
<property name="label" translatable="yes">About</property> <property name="label" translatable="yes">About</property>
@ -200,15 +209,12 @@
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">True</property> <property name="fill">True</property>
<property name="position">2</property> <property name="position">3</property>
</packing> </packing>
</child> </child>
<child> <child>
<placeholder/> <placeholder/>
</child> </child>
<child>
<placeholder/>
</child>
</object> </object>
<packing> <packing>
<property name="position">2</property> <property name="position">2</property>
@ -265,4 +271,9 @@
<placeholder/> <placeholder/>
</child> </child>
</object> </object>
<object class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-about</property>
</object>
</interface> </interface>

View File

@ -10,16 +10,10 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">False</property> <property name="can-focus">False</property>
</object> </object>
<object class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-about</property>
</object>
<object class="GtkPopover" id="popover1"> <object class="GtkPopover" id="popover1">
<property name="width-request">300</property> <property name="width-request">300</property>
<property name="height-request">400</property> <property name="height-request">400</property>
<property name="can-focus">False</property> <property name="can-focus">False</property>
<property name="modal">False</property>
<child> <child>
<object class="GtkNotebook"> <object class="GtkNotebook">
<property name="visible">True</property> <property name="visible">True</property>
@ -189,6 +183,20 @@
<property name="position">1</property> <property name="position">1</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkButton" id="btndraw">
<property name="label" translatable="yes">Drawing App</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="relief">none</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child> <child>
<object class="GtkButton" id="btnabout"> <object class="GtkButton" id="btnabout">
<property name="label" translatable="yes">About</property> <property name="label" translatable="yes">About</property>
@ -201,15 +209,12 @@
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">True</property> <property name="fill">True</property>
<property name="position">2</property> <property name="position">3</property>
</packing> </packing>
</child> </child>
<child> <child>
<placeholder/> <placeholder/>
</child> </child>
<child>
<placeholder/>
</child>
</object> </object>
<packing> <packing>
<property name="position">2</property> <property name="position">2</property>
@ -266,4 +271,9 @@
<placeholder/> <placeholder/>
</child> </child>
</object> </object>
<object class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-about</property>
</object>
</interface> </interface>

149
Gtk3/src/drawing.cpp Normal file
View File

@ -0,0 +1,149 @@
#include "drawing.h"
#include <cstdio>
typedef struct {
GtkWidget *draw_area1;
double x;
double y;
}MousePos;
static cairo_surface_t *surface=cairo_image_surface_create(CAIRO_FORMAT_ARGB32,640,360);
//static double start_x,start_y;
static void clear_surface (void)
{
cairo_t *cr;
cr = cairo_create (surface);
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_paint (cr);
cairo_destroy (cr);
}
/*
static void resize_cb(GtkWidget *widget,int width,int height,gpointer data){
cairo_surface_destroy (surface);
cairo_surface_create_similar(surface,CAIRO_CONTENT_COLOR,width,height);
}*/
static gboolean draw_cb(GtkWidget *widget,cairo_t *cr,gpointer data){
cairo_set_source_surface(cr,surface,0,0);
cairo_paint(cr);
//Set Color
/*guint width, height;
width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget);
cairo_arc (cr,
width / 2.0, height / 2.0,
MIN (width, height) / 2.0,
0, 2 * G_PI);
cairo_fill(cr);*/
return FALSE;
}
static void draw_brush(GtkWidget *widget,double x,double y){
cairo_t *cr;
//Draw on the surface
cr=cairo_create(surface);
//Some bugs in color set
GdkRGBA color;
//Draw and fill in color
cairo_arc(cr,x,y,3,0,2*G_PI);
//gdk_rgba_parse(&color,"66CCFF");
//gdk_cairo_set_source_rgba(cr,&color);
cairo_fill(cr);
cairo_destroy(cr);
gtk_widget_queue_draw(widget);
}
static void drag_begin(GtkGesture *gesture,double x,double y,MousePos *pos){
pos->x=x;
pos->y=y;
draw_brush(pos->draw_area1,x,y);
/*
freopen("drag.txt","a",stdout);
g_print("%lf %lf\n",x,y);
fclose(stdout);
*/
}
static void drag_update(GtkGesture *gesture,double x,double y,MousePos *pos){
draw_brush(pos->draw_area1,x+pos->x,y+pos->y);
/*
freopen("drag.txt","a",stdout);
g_print("%lf %lf\n",x+pos->x,y+pos->y);
fclose(stdout);
*/
}
static void drag_end(GtkGesture *gesture,double x,double y,MousePos *pos){
draw_brush(pos->draw_area1,x+pos->x,y+pos->y);
/*
freopen("drag.txt","a",stdout);
g_print("%lf %lf\n",x+pos->x,y+pos->y);
fclose(stdout);
*/
}
static void pressed(GtkGestureMultiPress *gesture,int n_press,double x,double y,GtkWidget *widget){
clear_surface();
gtk_widget_queue_draw(widget);
/*
g_print("%d\n",n_press);
freopen("drag.txt","w",stdout);
printf("\0");
fclose(stdout);
*/
}
static void close_window(void){
if(surface){
cairo_surface_destroy(surface);
}
}
void drawing_main(GtkWidget *widget,GtkWindow *parent){
GtkWidget *window,*draw_area;
GtkGesture *press,*drag;
//Create a window
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_transient_for(GTK_WINDOW(window),parent);
gtk_window_set_title(GTK_WINDOW(window),"Drawing Test");
gtk_window_set_default_size(GTK_WINDOW(window),640,360);
gtk_window_set_icon_name(GTK_WINDOW(window),"org.gtk.daleclack");
g_signal_connect(window,"destroy",G_CALLBACK(close_window),NULL);
//Create a draw area
draw_area=gtk_drawing_area_new();
g_signal_connect(draw_area,"draw",G_CALLBACK(draw_cb),NULL);
//g_signal_connect_after(draw_area,"resize",G_CALLBACK(resize_cb),NULL);
//Create gestures
static MousePos pos;
pos.draw_area1=draw_area;
//"Drag" gesture
drag=gtk_gesture_drag_new(draw_area);
gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(drag),GDK_BUTTON_PRIMARY);
g_signal_connect(drag,"drag-begin",G_CALLBACK(drag_begin),&pos);
g_signal_connect(drag,"drag-update",G_CALLBACK(drag_update),&pos);
g_signal_connect(drag,"drag-end",G_CALLBACK(drag_end),&pos);
//"Press" gesture
press=gtk_gesture_multi_press_new(draw_area);
gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(press),GDK_BUTTON_SECONDARY);
g_signal_connect(press,"pressed",G_CALLBACK(pressed),draw_area);
gtk_container_add(GTK_CONTAINER(window),draw_area);
gtk_widget_show_all(window);
}

8
Gtk3/src/drawing.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef __DRAWING_H_
#define __DRAWING_H_
#include <gtk/gtk.h>
void drawing_main(GtkWidget *widget,GtkWindow *parent);
#endif

View File

@ -4,6 +4,7 @@
#include "win1.h" #include "win1.h"
#include "game.h" #include "game.h"
#include "TextEditor.h" #include "TextEditor.h"
#include "drawing.h"
static void btnvlc_clicked(GtkWidget *widget,gpointer data){ static void btnvlc_clicked(GtkWidget *widget,gpointer data){
std::thread first(system,"vlc"); std::thread first(system,"vlc");
@ -82,6 +83,10 @@ void add_leftpanel(GtkBuilder *builder,GtkFixed *fixed){
GObject *btnedit=gtk_builder_get_object(panel2,"btneditor"); GObject *btnedit=gtk_builder_get_object(panel2,"btneditor");
g_signal_connect(btnedit,"clicked",G_CALLBACK(text_editor),window); g_signal_connect(btnedit,"clicked",G_CALLBACK(text_editor),window);
g_signal_connect_swapped(btnedit,"clicked",G_CALLBACK(gtk_widget_hide),popover); g_signal_connect_swapped(btnedit,"clicked",G_CALLBACK(gtk_widget_hide),popover);
//Drawing application
GObject *btndraw=gtk_builder_get_object(panel2,"btndraw");
g_signal_connect(btndraw,"clicked",G_CALLBACK(drawing_main),window);
g_signal_connect_swapped(btndraw,"clicked",G_CALLBACK(gtk_widget_hide),popover);
//About window //About window
GObject *btn_about=gtk_builder_get_object(panel2,"btnabout"); GObject *btn_about=gtk_builder_get_object(panel2,"btnabout");
g_signal_connect(btn_about,"clicked",G_CALLBACK(win1_init),window); g_signal_connect(btn_about,"clicked",G_CALLBACK(win1_init),window);