|
| 1 | +""" |
| 2 | +
|
| 3 | +raylib [texture] example - Mouse Painting |
| 4 | +
|
| 5 | +""" |
| 6 | +from pyray import * |
| 7 | +from raylib.colors import * |
| 8 | +from raylib import ( |
| 9 | + KEY_RIGHT, |
| 10 | + KEY_LEFT, |
| 11 | + MOUSE_BUTTON_LEFT, |
| 12 | + KEY_C, |
| 13 | + GESTURE_DRAG, |
| 14 | + MOUSE_BUTTON_RIGHT, |
| 15 | + KEY_S |
| 16 | +) |
| 17 | + |
| 18 | +MAX_COLORS_COUNT = 23 # Number of colors available |
| 19 | + |
| 20 | +# Initialization |
| 21 | +screenWidth = 800 |
| 22 | +screenHeight = 450 |
| 23 | + |
| 24 | +init_window(screenWidth, screenHeight, "raylib [textures] example - mouse painting") |
| 25 | + |
| 26 | +# Colours to choose from |
| 27 | +colors = [RAYWHITE, YELLOW, GOLD, ORANGE, PINK, RED, MAROON, GREEN, LIME, DARKGREEN, |
| 28 | + SKYBLUE, BLUE, DARKBLUE, PURPLE, VIOLET, DARKPURPLE, BEIGE, BROWN, DARKBROWN, |
| 29 | + LIGHTGRAY, GRAY, DARKGRAY, BLACK] |
| 30 | + |
| 31 | +colorsRecs = [] |
| 32 | + |
| 33 | +# Define colorsRecs data (for every rectangle) |
| 34 | +for i in range(MAX_COLORS_COUNT): |
| 35 | + colorsRecs.append(Rectangle(10 + 30.0 * i + 2 * i, 10, 30, 30)) |
| 36 | + |
| 37 | +colorSelected = 0 |
| 38 | +colorSelectedPrev = colorSelected |
| 39 | +colorMouseHover = 0 |
| 40 | +brushSize = 20.0 |
| 41 | +mouseWasPressed = False |
| 42 | + |
| 43 | +btnSaveRec = Rectangle(750, 10, 40, 30) |
| 44 | +btnSaveMouseHover = False |
| 45 | +showSaveMessage = False |
| 46 | +saveMessageCounter = 0 |
| 47 | + |
| 48 | +# Create a RenderTexture2D to use as a canvas |
| 49 | +target = load_render_texture(screenWidth, screenHeight) |
| 50 | + |
| 51 | +# Clear render texture before entering the game loop |
| 52 | +begin_texture_mode(target) |
| 53 | +clear_background(colors[0]) |
| 54 | +end_texture_mode() |
| 55 | + |
| 56 | +set_target_fps(120) # Set our game to run at 120 frames-per-second |
| 57 | + |
| 58 | +# Main game loop |
| 59 | +while not window_should_close(): # Detect window close button or ESC key |
| 60 | + # Update |
| 61 | + # ---------------------------------------------------------------------------------- |
| 62 | + mousePos = get_mouse_position() |
| 63 | + |
| 64 | + # Move between colors with keys |
| 65 | + if is_key_pressed(KEY_RIGHT): |
| 66 | + colorSelected += 1 |
| 67 | + elif is_key_pressed(KEY_LEFT): |
| 68 | + colorSelected -= 1 |
| 69 | + |
| 70 | + if colorSelected >= MAX_COLORS_COUNT: |
| 71 | + colorSelected = MAX_COLORS_COUNT - 1 |
| 72 | + elif colorSelected < 0: |
| 73 | + colorSelected = 0 |
| 74 | + |
| 75 | + # Choose color with mouse |
| 76 | + for i in range(MAX_COLORS_COUNT): |
| 77 | + if check_collision_point_rec(mousePos, colorsRecs[i]): |
| 78 | + colorMouseHover = i |
| 79 | + break |
| 80 | + else: |
| 81 | + colorMouseHover = -1 |
| 82 | + |
| 83 | + if colorMouseHover >= 0 and is_mouse_button_pressed(MOUSE_BUTTON_LEFT): |
| 84 | + colorSelected = colorMouseHover |
| 85 | + colorSelectedPrev = colorSelected |
| 86 | + |
| 87 | + # Change brush size |
| 88 | + brushSize += get_mouse_wheel_move() * 5 |
| 89 | + if brushSize < 2: brushSize = 2 |
| 90 | + if brushSize > 50: brushSize = 50 |
| 91 | + |
| 92 | + if is_key_pressed(KEY_C): |
| 93 | + # Clear render texture to clear color |
| 94 | + begin_texture_mode(target) |
| 95 | + clear_background(colors[0]) |
| 96 | + end_texture_mode() |
| 97 | + |
| 98 | + if is_mouse_button_pressed(MOUSE_BUTTON_LEFT) or get_gesture_detected() == GESTURE_DRAG: |
| 99 | + |
| 100 | + # Paint circle into render texture |
| 101 | + # NOTE: To avoid discontinuous circles, we could store |
| 102 | + # previous-next mouse points and just draw a line using brush size |
| 103 | + begin_texture_mode(target) |
| 104 | + if mousePos.y > 50: |
| 105 | + draw_circle(int(mousePos.x), int(mousePos.y), brushSize, colors[colorSelected]) |
| 106 | + end_texture_mode() |
| 107 | + if is_mouse_button_down(MOUSE_BUTTON_RIGHT): |
| 108 | + |
| 109 | + if not mouseWasPressed: |
| 110 | + colorSelectedPrev = colorSelected |
| 111 | + colorSelected = 0 |
| 112 | + |
| 113 | + mouseWasPressed = True |
| 114 | + |
| 115 | + # Erase circle from render texture |
| 116 | + begin_texture_mode(target) |
| 117 | + if mousePos.y > 50: draw_circle(int(mousePos.x), int(mousePos.y), brushSize, colors[0]) |
| 118 | + end_texture_mode() |
| 119 | + |
| 120 | + elif is_mouse_button_released(MOUSE_BUTTON_RIGHT) and mouseWasPressed: |
| 121 | + |
| 122 | + colorSelected = colorSelectedPrev |
| 123 | + mouseWasPressed = False |
| 124 | + |
| 125 | + # Check mouse hover save button |
| 126 | + if check_collision_point_rec(mousePos, btnSaveRec): |
| 127 | + btnSaveMouseHover = True |
| 128 | + else: |
| 129 | + btnSaveMouseHover = False |
| 130 | + |
| 131 | + # Image saving logic |
| 132 | + # NOTE: Saving painted texture to a default named image |
| 133 | + if (btnSaveMouseHover and is_mouse_button_released(MOUSE_BUTTON_LEFT)) or is_key_pressed(KEY_S): |
| 134 | + image = load_image_from_texture(target.texture) |
| 135 | + image_flip_vertical(image) |
| 136 | + export_image(image, "my_amazing_texture_painting.png") |
| 137 | + unload_image(image) |
| 138 | + showSaveMessage = True |
| 139 | + |
| 140 | + if showSaveMessage: |
| 141 | + # On saving, show a full screen message for 2 seconds |
| 142 | + saveMessageCounter += 1 |
| 143 | + if saveMessageCounter > 240: |
| 144 | + showSaveMessage = False |
| 145 | + saveMessageCounter = 0 |
| 146 | + |
| 147 | + # ---------------------------------------------------------------------------------- |
| 148 | + |
| 149 | + # Draw |
| 150 | + # ---------------------------------------------------------------------------------- |
| 151 | + begin_drawing() |
| 152 | + clear_background(RAYWHITE) |
| 153 | + |
| 154 | + # NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) |
| 155 | + draw_texture_rec(target.texture, Rectangle(0, 0, float(target.texture.width), float(-target.texture.height)), |
| 156 | + Vector2(0, 0), WHITE) |
| 157 | + |
| 158 | + # Draw drawing circle for reference |
| 159 | + if mousePos.y > 50: |
| 160 | + if is_mouse_button_down(MOUSE_BUTTON_RIGHT): |
| 161 | + draw_circle_lines(int(mousePos.x), int(mousePos.y), brushSize, GRAY) |
| 162 | + else: |
| 163 | + draw_circle(get_mouse_x(), get_mouse_y(), brushSize, colors[colorSelected]) |
| 164 | + # Draw top panel |
| 165 | + draw_rectangle(0, 0, get_screen_width(), 50, RAYWHITE) |
| 166 | + draw_line(0, 50, get_screen_width(), 50, LIGHTGRAY) |
| 167 | + |
| 168 | + # Draw color selection rectangles |
| 169 | + for i in range(MAX_COLORS_COUNT): |
| 170 | + draw_rectangle_rec(colorsRecs[i], colors[i]) |
| 171 | + draw_rectangle_lines(10, 10, 30, 30, LIGHTGRAY) |
| 172 | + |
| 173 | + if colorMouseHover >= 0: draw_rectangle_rec(colorsRecs[colorMouseHover], fade(WHITE, 0.6)) |
| 174 | + |
| 175 | + draw_rectangle_lines_ex( |
| 176 | + Rectangle(colorsRecs[colorSelected].x - 2, colorsRecs[colorSelected].y - 2, colorsRecs[colorSelected].width + 4, |
| 177 | + colorsRecs[colorSelected].height + 4), 2, BLACK) |
| 178 | + |
| 179 | + # Draw save image button |
| 180 | + draw_rectangle_lines_ex(btnSaveRec, 2, RED if btnSaveMouseHover else BLACK) |
| 181 | + draw_text("SAVE!", 755, 20, 10, RED if btnSaveMouseHover else BLACK) |
| 182 | + |
| 183 | + if showSaveMessage: |
| 184 | + draw_rectangle(0, 0, get_screen_width(), get_screen_height(), fade(RAYWHITE, 0.8)) |
| 185 | + draw_rectangle(0, 150, get_screen_width(), 80, BLACK) |
| 186 | + draw_text("IMAGE SAVED: my_amazing_texture_painting.png", 150, 180, 20, RAYWHITE) |
| 187 | + |
| 188 | + end_drawing() |
| 189 | + |
| 190 | + # ---------------------------------------------------------------------------------- |
| 191 | + |
| 192 | +# De-Initialization |
| 193 | +# ---------------------------------------------------------------------------------- |
| 194 | +unload_render_texture(target) # Unload render texture |
| 195 | + |
| 196 | +close_window() # Close window and OpenGL context |
0 commit comments