raylib/ports/shapes/shapes_rectangle_scaling

Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license. Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
A screenshot of shapes_rectangle_scaling

Code

shapes_rectangle_scaling.odin ¶
113 linesSource

1/*******************************************************************************************
2*
3*   raylib [shapes] example - rectangle scaling
4*
5*   Example complexity rating: [★★☆☆] 2/4
6*
7*   Example originally created with raylib 2.5, last time updated with raylib 2.5
8*
9*   Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
10*
11*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12*   BSD-like license that allows static linking with closed source software
13*
14*   Copyright (c) 2018-2025 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
15*
16********************************************************************************************/
17
18package raylib_examples
19
20import rl "vendor:raylib"
21
22MOUSE_SCALE_MARK_SIZE :: 12
23
24//------------------------------------------------------------------------------------
25// Program main entry point
26//------------------------------------------------------------------------------------
27main :: proc() {
28	// Initialization
29	//--------------------------------------------------------------------------------------
30	SCREEN_WIDTH :: 800
31	SCREEN_HEIGHT :: 450
32
33	rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - rectangle scaling")
34
35	rec := rl.Rectangle {100, 100, 200, 80}
36
37	mouse_position: rl.Vector2
38
39	mouse_scale_ready: bool
40	mouse_scale_mode: bool
41
42	rl.SetTargetFPS(60)               // Set our game to run at 60 frames-per-second
43	//--------------------------------------------------------------------------------------
44
45	// Main game loop
46	for !rl.WindowShouldClose() {    // Detect window close button or ESC key
47		// Update
48		//----------------------------------------------------------------------------------
49		mouse_position = rl.GetMousePosition()
50
51		if rl.CheckCollisionPointRec(mouse_position, {rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE}) {
52			mouse_scale_ready = true
53			if rl.IsMouseButtonPressed(.LEFT) {
54				mouse_scale_mode = true
55			}
56		} else {
57			mouse_scale_ready = false
58		}
59
60		if mouse_scale_mode {
61			mouse_scale_ready = true
62
63			rec.width = (mouse_position.x - rec.x)
64			rec.height = (mouse_position.y - rec.y)
65
66			// Check minimum rec size
67			if rec.width < MOUSE_SCALE_MARK_SIZE {
68				rec.width = MOUSE_SCALE_MARK_SIZE
69			}
70			if rec.height < MOUSE_SCALE_MARK_SIZE {
71				rec.height = MOUSE_SCALE_MARK_SIZE
72			}
73
74			// Check maximum rec size
75			if rec.width > (f32(rl.GetScreenWidth()) - rec.x) {
76				rec.width = f32(rl.GetScreenWidth()) - rec.x
77			}
78			if rec.height > (f32(rl.GetScreenHeight()) - rec.y) {
79				rec.height = f32(rl.GetScreenHeight()) - rec.y
80			}
81
82			if rl.IsMouseButtonReleased(.LEFT) {
83				mouse_scale_mode = false
84			}
85		}
86		//----------------------------------------------------------------------------------
87
88		// Draw
89		//----------------------------------------------------------------------------------
90		rl.BeginDrawing()
91
92			rl.ClearBackground(rl.RAYWHITE)
93
94			rl.DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, rl.GRAY)
95
96			rl.DrawRectangleRec(rec, rl.Fade(rl.GREEN, 0.5))
97
98			if mouse_scale_ready {
99				rl.DrawRectangleLinesEx(rec, 1, rl.RED)
100				rl.DrawTriangle({rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height},
101							 {rec.x + rec.width, rec.y + rec.height},
102							 {rec.x + rec.width, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE}, rl.RED)
103			}
104
105		rl.EndDrawing()
106		//----------------------------------------------------------------------------------
107	}
108
109	// De-Initialization
110	//--------------------------------------------------------------------------------------
111	rl.CloseWindow()        // Close window and OpenGL context
112	//--------------------------------------------------------------------------------------
113}

Declarations Used 24