sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
aaedit.c
1#include <string.h>
2#include <ctype.h>
3#include <malloc.h>
4#include "aalib.h"
5static void aa_editdisplay(struct aa_edit *e)
6{
7 char s[1000];
8 int i;
9 if (e->cursor > strlen(e->data))
10 e->cursor = strlen(e->data);
11 if (e->cursor < e->printpos)
12 e->printpos = e->cursor;
13 if (e->cursor >= e->printpos + e->size)
14 e->printpos = e->cursor - e->size;
15 if (e->printpos < 0)
16 e->printpos = 0;
17 strncpy(s, e->data + e->printpos, e->size);
18 s[e->size] = 0;
19 for (i = strlen(e->data) - e->printpos; i < e->size; i++)
20 s[i] = ' ';
21 aa_puts(e->c, e->x, e->y, e->clearafterpress ? AA_REVERSE : AA_SPECIAL, s);
22 aa_gotoxy(e->c, e->x + e->cursor - e->printpos, e->y);
23}
24struct aa_edit *aa_createedit(aa_context * c, int x, int y, int size, char *s, int maxsize)
25{
26 struct aa_edit *e;
27 if (x < 0)
28 x = 0;
29 if (y < 0)
30 y = 0;
31 if (x >= aa_imgwidth(c) - 1)
32 x = aa_imgwidth(c) - 2;
33 if (y >= aa_imgheight(c) - 1)
34 y = aa_imgwidth(c) - 2;
35 if (x + size >= aa_imgwidth(c))
36 size = aa_imgwidth(c) - 1 - x;
37 e = malloc(sizeof(struct aa_edit));
38 if (e == NULL)
39 return NULL;
40 e->maxsize = maxsize;
41 e->data = s;
42 e->cursor = strlen(s);
43 e->clearafterpress = 1;
44 e->x = x;
45 e->y = y;
46 e->size = size;
47 e->c = c;
48 e->printpos = 0;
49 aa_editdisplay(e);
50 return (e);
51}
52static void aa_insert(struct aa_edit *e, char ch)
53{
54 int i, s = strlen(e->data);
55 if (s == e->maxsize - 1)
56 return;
57 for (i = s; i >= e->cursor; i--) {
58 e->data[i + 1] = e->data[i];
59 }
60 e->data[s + 1] = 0;
61 e->data[e->cursor] = ch;
62 e->cursor++;
63}
64static void aa_delete(struct aa_edit *e)
65{
66 int i, s = strlen(e->data);
67 if (e->cursor == 0)
68 return;
69 e->cursor--;
70 for (i = e->cursor; i <= s; i++) {
71 e->data[i] = e->data[i + 1];
72 }
73}
74void aa_editkey(struct aa_edit *e, int c)
75{
76 if (c < 127 && (isgraph(c) || c == ' ')) {
77 if (e->clearafterpress)
78 e->data[0] = 0,
79 e->cursor = 0;
80 e->clearafterpress = 0;
81 aa_insert(e, c);
82 aa_editdisplay(e);
83 } else if (c == AA_BACKSPACE) {
84 e->clearafterpress = 0;
85 aa_delete(e);
86 aa_editdisplay(e);
87 } else if (c == AA_LEFT) {
88 e->cursor--;
89 e->clearafterpress = 0;
90 if (e->cursor < 0)
91 e->cursor = 0;
92 aa_editdisplay(e);
93 } else if (c == AA_RIGHT) {
94 e->cursor++;
95 e->clearafterpress = 0;
96 if (e->cursor > strlen(e->data))
97 e->cursor = strlen(e->data);
98 aa_editdisplay(e);
99 }
100 aa_flush(e->c);
101}
102void aa_edit(aa_context * c, int x, int y, int size, char *s, int maxsize)
103{
104 struct aa_edit *e;
105 int ch;
106 aa_showcursor(c);
107 e = aa_createedit(c, x, y, size, s, maxsize);
108 aa_flush(c);
109 while ((ch = aa_getkey(c, 1)) != 13 && ch != '\n') {
110 aa_editkey(e, ch);
111 }
112 aa_hidecursor(c);
113 free(e);
114}