Moving a box with the mouse

closed account (GbX36Up4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include "header.h"
using namespace std;

BITMAP *buffer;

class TextBox{
public:
	void DrawTextBox();
	void CheckClick();
	void Click();
	void SetVals(int a, int b);
private:
	int x, x2;
	int y, y2;
	string l1;
	string l2;
	string l3;
	bool click;
};

void TextBox::DrawTextBox(){
	rect(buffer, x, y, x+160, y+30, makecol(255, 0, 0));
}

void TextBox::CheckClick(){
	if(mouse_b & 1){
		if(mouse_x > x && mouse_x < x+160 && mouse_y > y && mouse_y < y+30)
		{
			x2 = mouse_x;
			y2 = mouse_y;
			click = true;
		}
	}
	else{
		click = false;
	}
}

void TextBox::Click(){
	if(click == true){
		x = x + (mouse_x - x2);
		y = y + (mouse_y - y2);
	}
}

void TextBox::SetVals(int a, int b){
	click = false;
	x = a;
	y = b;
}

int wmain(void){
	allegro_init();
	install_keyboard();
	install_mouse();
	set_color_depth(32);
	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1024, 768, 0, 0);
	buffer = create_bitmap(1024, 768);
	int mx, my;

	TextBox txtbox;
	txtbox.SetVals(1, 1);
	while(!key[KEY_ESC]){
		mx = mouse_x;
		my = mouse_y;

		txtbox.CheckClick();
		txtbox.Click();
		txtbox.DrawTextBox();

		circlefill(buffer, mx, my, 3, makecol(0, 255, 0));
		draw_sprite(screen, buffer, 0, 0);
		clear_to_color(buffer, makecol(255, 255, 255));
	}

	destroy_bitmap(buffer);
	return 0;
}


Above I have some pretty basic code written with Allegro. Basically I'm trying to get it so that if the user presses the left mouse button and is in the text box they can move it with their mouse. So far it works except for two small problems. 1.) Instead of the box moving whenever the mouse moves, it only moves when the mouse is moving AND at the edge of the box. 2.) Sometimes if the mouse if being moved a lot the box will just fly off the screen.
Topic archived. No new replies allowed.