Exemplo n.º 1
0
	void	root::set_drag_state(character::drag_state& ds)
	{
		m_drag_state = ds;

		// do we need to calculate the offset?
		character* ch = ds.GetCharacter();
		if (ch == NULL || ds.IsLockCentered())
		{
			return;
		}

		// get the character's origin
		point origin(0, 0);
		matrix chMatrix = ch->get_world_matrix();
		point worldOrigin;
		chMatrix.transform( &worldOrigin, origin );

		// Get current mouse coordinates
		int x, y, buttons;
		get_mouse_state(&x, &y, &buttons);

		// ... in 'world' space (i.e. twips)
		point worldMouse(PIXELS_TO_TWIPS(x), PIXELS_TO_TWIPS(y));

		// we need the offset from the origin of the character
		float xOffset = worldMouse.m_x - worldOrigin.m_x;
		float yOffset = worldMouse.m_y - worldOrigin.m_y;
		m_drag_state.SetOffset(xOffset, yOffset);
	}
Exemplo n.º 2
0
	// Implement mouse-dragging for this movie.
	void	root::do_mouse_drag()		
	{
		character* draggingChar = m_drag_state.GetCharacter();
		if (draggingChar == NULL)
		{
			return;
		}

		// handle if the character isn't valid anymore
		if (draggingChar->is_alive() == false)
		{
			// no longer valid
			m_drag_state.Reset();
			return;
		}

		// get the current mouse
		int	x, y, buttons;
		get_mouse_state( &x, &y, &buttons );

		// ... in world coordinates (twips)
		point worldMouse( PIXELS_TO_TWIPS(x), PIXELS_TO_TWIPS(y) );

		matrix parentWorldMat;
		if (draggingChar->m_parent != NULL)
		{
			parentWorldMat = draggingChar->m_parent->get_world_matrix();
		}

		// if we're not locked to the center - adjust by the offset we started dragging by
		if(m_drag_state.IsLockCentered() == false)
		{
			worldMouse.m_x -= m_drag_state.OffsetX();
			worldMouse.m_y -= m_drag_state.OffsetY();
		}

		rect origBoundRect;
		if (m_drag_state.GetBounds(&origBoundRect))
		{
			// bounds are in local coordinate space
			rect bounds;
			bounds.enclose_transformed_rect( parentWorldMat, origBoundRect );

			// Clamp mouse coords within a defined rect.
			worldMouse.m_x = fclamp(worldMouse.m_x, bounds.m_x_min, bounds.m_x_max);
			worldMouse.m_y = fclamp(worldMouse.m_y, bounds.m_y_min, bounds.m_y_max);
		}

		point parentMouse;
		parentWorldMat.transform_by_inverse( &parentMouse, worldMouse );

		// Place our origin so that it coincides with the mouse coords
		// in our parent frame.
		matrix local = draggingChar->get_matrix();
		local.m_[0][2] = parentMouse.m_x;//set translation x
		local.m_[1][2] = parentMouse.m_y;//set translation y
		draggingChar->set_matrix( local );
	}
Exemplo n.º 3
0
int main_menu()
{
	continue_condition = true;
	fade_condition = false;
	function_to_call = NULL;
	darken = 127;
	int failure = 0;
	initialize_main_menu_buttons();
	int ipc_status;
	message msg;
	int r;
	char * display_name = (char*)malloc(sizeof(char) * strlen("Player name: ")+MAX_PLAYER_NAME_LENGTH);
	strcpy(display_name, "Player name: ");
	display_name = strcat(display_name, get_program_playername());
	if(!failure){
		do{
			/* Get a request message. */
			if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) {
				printf("driver_receive failed with: %d", r);
				continue;
			}
			if (is_ipc_notify(ipc_status)) { /* received notification */
				switch (_ENDPOINT_P(msg.m_source)) {
				case HARDWARE: /* hardware interrupt notification */
					if (msg.NOTIFY_ARG & get_kbd_irq_set()){  /* subscribed kbd interrupt */
						kbd_int_handler();
					}
					if (msg.NOTIFY_ARG & get_rtc_irq_set()) { /* subscribed timer interrupt */
						if(rtc_ih())
							failure = 1;
						getDateString(get_date_str_ptr());
					}
					if(msg.NOTIFY_ARG & get_timer_irq_set()){
						timer_int_handler();
						main_menu_render();
					}
					if (msg.NOTIFY_ARG & get_mouse_irq_set()) { /* subscribed timer interrupt */
						mouse_int_handler();
				}
					break;
				default:
					break; /* no other notifications expected: do nothing */
				}
			} else {/* received a standard message, not a notification */
				/* no standard messages expected: do nothing */
			}
			if(mouse_is_updated())
			{
				assign_mouse(get_previous_mouse(), get_mouse());
				assign_mouse(get_mouse(), get_mouse_state());
				main_menu_mouse_event(get_previous_mouse(), get_mouse());
				move_cursor(get_cursor(), get_mouse()->coords);
			}
		} while(continue_condition);
	}
	return failure;
}
Exemplo n.º 4
0
/*
 * Program entry point
 */
uint main(uint argc, uchar* argv[])
{
  uchar* title_info = "L:     F1:Save ESC:Exit"; /* const */
  uchar  line_ibcd[4]; /* To store line number digits */
  uint   ibcdt;

  uint i = 0;
  uint n = 0;
  uint result = 0;

  /* buff is fixed size and allocated in far memory so it
   * can be big enough.
   * buff_size is the size in bytes actually used in buff
   * buff_cursor_offset is the linear offset of current
   * cursor position inside buff
   */
  lp_t buff = 0;
  ul_t buff_size = 0;
  ul_t buff_cursor_offset = 0;

  /* First line number to display in the editor area */
  uint current_line = 0;

  /* Var to get key presses */
  uint k = 0;

  struct FS_ENTRY entry;

  /* Chck usage */
  if(argc != 2) {
    putstr("Usage: %s <file>\n\r\n\r", argv[0]);
    putstr("<file> can be:\n\r");
    putstr("-an existing file path: opens existing file to edit\n\r");
    putstr("-a new file path: opens empty editor. File will be created on save\n\r");
    putstr("\n\r");
    return 1;
  }

  /* Allocate fixed size text buffer */
  buff = lmalloc(0xFFFFL);
  if(buff == 0) {
    putstr("Error: can't allocate memory\n\r");
    return 1;
  }

  /* Find file */
  n = get_entry(&entry, argv[1], UNKNOWN_VALUE, UNKNOWN_VALUE);

  /* Load file or show error */
  if(n<ERROR_ANY && (entry.flags & FST_FILE)) {
    ul_t offset = 0;
    uchar cbuff[512];
    setlc(buff, entry.size, 0);
    buff_size = entry.size;
    while(result = read_file(cbuff, argv[1], (uint)offset, sizeof(cbuff))) {
      if(result >= ERROR_ANY) {
        lmfree(buff);
        putstr("Can't read file %s (error=%x)\n\r", argv[1], result);
        return 1;
      }
      lmemcpy(buff + offset, lp(cbuff), (ul_t)result);
      offset += result;
    }
    if(offset != entry.size) {
      lmfree(buff);
      putstr("Can't read file (readed %d bytes, expected %d)\n\r",
        (uint)offset, entry.size);
      return 1;
    }
    /* Buffer must finish with a 0 and */
    /* must fit at least this 0, so buff_size can't be 0 */
    if(buff_size == 0 || getlc(buff + buff_size-1L) != 0) {
      buff_size++;
    }
  }

  /* Create 1 byte buffer if this is a new file */
  /* This byte is for the final 0 */
  if(buff_size == 0) {
    buff_size = 1;
    lmemset(buff, 0, buff_size);
  }

  /* Get screen size */
  get_screen_size(SSM_CHARS, &SCREEN_WIDTH, &SCREEN_HEIGHT);

  /* Allocate screen buffer */
  screen_buff = lmalloc((ul_t)(SCREEN_WIDTH*(SCREEN_HEIGHT-1)));
  if(screen_buff == 0) {
    putstr("Error: can't allocate memory\n\r");
    lmfree(buff);
    return 1;
  }

  /* Clear screen buffer */
  lmemset(screen_buff, 0, (ul_t)(SCREEN_WIDTH*(SCREEN_HEIGHT-1)));

  /* Write title */
  for(i=0; i<strlen(argv[1]); i++) {
    putchar_attr(i, 0, argv[1][i], TITLE_ATTRIBUTES);
  }
  for(; i<SCREEN_WIDTH-strlen(title_info); i++) {
    putchar_attr(i, 0, ' ', TITLE_ATTRIBUTES);
  }
  for(; i<SCREEN_WIDTH; i++) {
    putchar_attr(i, 0,
      title_info[i+strlen(title_info)-SCREEN_WIDTH], TITLE_ATTRIBUTES);
  }

  /* Show buffer and set cursor at start */
  set_show_cursor(HIDE_CURSOR);
  show_buffer_at_line(buff, current_line);
  set_cursor_position(0, 1);
  set_show_cursor(SHOW_CURSOR);

  /* Main loop */
  while(k != KEY_ESC) {
    uint col, line;

    /* Getmouse state */
    get_mouse_state(SSM_CHARS, &mouse_x, &mouse_y, &mouse_buttons);

    /* Process buttons */
    if(mouse_buttons & MOUSE_LEFT_BUTTON) {
      buff_cursor_offset = linecol_to_buffer_offset(buff, mouse_x, mouse_y-1);
    }

    /* Get key press */
    k = getkey(KM_NO_WAIT);

    /* Process key actions */

    /* Keys to ignore */
    if((k>KEY_F1 && k<=KEY_F10) ||
      k==KEY_F11 || k==KEY_F12 ||
      k==KEY_PRT_SC || k==KEY_INS ||
      k==0) {
      continue;

    /* Key F1: Save */
    } else if(k == KEY_F1) {
      ul_t offset = 0;
      uchar cbuff[512];
      result = 0;
      while(offset<buff_size && result<ERROR_ANY) {
        ul_t to_copy = min(sizeof(cbuff), buff_size-offset);
        lmemcpy(lp(cbuff), buff + offset, to_copy);
        result = write_file(cbuff, argv[1], (uint)offset, (uint)to_copy, FWF_CREATE | FWF_TRUNCATE);
        offset += to_copy;
      }

      /* Update state indicator */
      if(result < ERROR_ANY) {
        putchar_attr(strlen(argv[1]), 0, ' ', TITLE_ATTRIBUTES);
      } else {
        putchar_attr(strlen(argv[1]), 0, '*', (TITLE_ATTRIBUTES&0xF0)|AT_T_RED);
      }
      /* This opperation takes some time, so clear keyboard buffer */
      getkey(KM_CLEAR_BUFFER);

    /* Cursor keys: Move cursor */
    } else if(k == KEY_UP) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      if(line > 0) {
        line -= 1;
        buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);
      }

    } else if(k == KEY_DOWN) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      line += 1;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_LEFT) {
      if(buff_cursor_offset > 0) {
        buff_cursor_offset--;
      }

    } else if(k == KEY_RIGHT) {
      if(buff_cursor_offset < buff_size - 1) {
        buff_cursor_offset++;
      }

    /* HOME, END, PG_UP and PG_DN keys */
    } else if(k == KEY_HOME) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      col = 0;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_END) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      col = 0xFFFF;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_PG_DN) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      line += SCREEN_HEIGHT-1;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_PG_UP) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      line -= min(line, SCREEN_HEIGHT-1);
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);


    /* Backspace key: delete char before cursor and move cursor there */
    } else if(k == KEY_BACKSPACE) {
      if(buff_cursor_offset > 0) {
        lmemcpy(buff+buff_cursor_offset-1L, buff+buff_cursor_offset, buff_size-buff_cursor_offset);
        buff_size--;
        putchar_attr(strlen(argv[1]), 0, '*', TITLE_ATTRIBUTES);
        buff_cursor_offset--;
      }

    /* Del key: delete char at cursor */
    } else if(k == KEY_DEL) {
      if(buff_cursor_offset < buff_size-1) {
        lmemcpy(buff+buff_cursor_offset, buff+buff_cursor_offset+1, buff_size-buff_cursor_offset-1);
        buff_size--;
        putchar_attr(strlen(argv[1]), 0, '*', TITLE_ATTRIBUTES);
      }

    /* Any other key but esc: insert char at cursor */
    } else if(k != KEY_ESC && k != 0) {

      if(k == KEY_RETURN) {
        k = '\n';
      }
      if(k == KEY_TAB) {
        k = '\t';
      }
      lmemcpy(buff+buff_cursor_offset+1, buff+buff_cursor_offset, buff_size-buff_cursor_offset);
      setlc(buff, buff_cursor_offset++, k);
      buff_size++;
      putchar_attr(strlen(argv[1]), 0, '*', TITLE_ATTRIBUTES);
    }

    /* Update cursor position and display */
    buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
    if(line < current_line) {
      current_line = line;
    } else if(line > current_line + SCREEN_HEIGHT - 2) {
      current_line = line - SCREEN_HEIGHT + 2;
    }

    /* Update line number in title */
    /* Compute bcd value (reversed) */
    ibcdt = min(9999, buffer_offset_to_fileline(buff, buff_cursor_offset)+1);
    n = SCREEN_WIDTH-strlen(title_info)+2;
    for(i=0; i<4; i++) {
      line_ibcd[i] = ibcdt%10;
      ibcdt /= 10;
      if(ibcdt==0) {
        ibcdt = i;
        break;
      }
    }
    /* Display it */
    for(i=0; i<4; i++) {
      uchar c = i<=ibcdt?line_ibcd[ibcdt-i]+'0':' ';
      putchar_attr(n+i, 0, c, TITLE_ATTRIBUTES);
    }

    set_show_cursor(HIDE_CURSOR);
    show_buffer_at_line(buff, current_line);
    line -= current_line;
    line += 1;
    set_cursor_position(col, line);
    set_show_cursor(SHOW_CURSOR);
  }

  /* Free screen buffer */
  lmfree(screen_buff);

  /* Free buffer */
  lmfree(buff);

  /* Reset screen */
  clear_screen();
  set_cursor_position(0, 0);

  return 0;
}
Exemplo n.º 5
0
int mp_lobby()
{
	int failure = 0;
	if((set_com1_irq_set(generic_ser_subscribe_int_tx_rx(COM1))) < 0)
	{//subscribe interrupts
		printf("mp_lobby(): generic_ser_subscribe_int_tx_rx() failed \n");
		failure = 1;
	}
	if(SET_COMMUNICATION_PARAM())
	{
		printf("ser_set() failed\n");
		return 1;
	}
	initialize_lobby_buttons();
	set_role(SEARCHING);
	lobby_state = NOT_READY;
	enable_per_second_alarm(on_rtc_alarm_int_transmission);
	if(im_message_log != NULL)
		{
			size_t del_i;
			for(del_i = 0 ; del_i < size(im_message_log); del_i++)
			{
				free(*(char**)at(im_message_log, del_i));
			}
			delete_vector_t(im_message_log);
		}
	im_message_log = new_vector_t(sizeof(char*));
	memset(current_im, 0, sizeof(char)*IM_MESSAGE_MAX);
	current_im_index = 0;
	draw_im_index = 0;
	int wait = 0;
	function_to_call = NULL;
	continue_condition = true;
	fade_condition = false;
	darken = FADE_MAX;
	int ipc_status;
	message msg;
	int r;
	set_program_player2name((char*)malloc(sizeof(char)*MAX_PLAYER_NAME_LENGTH+1));
	if(get_program_playername2() == NULL)
		return 1;
	memset(get_program_playername2(),0,sizeof(char)*MAX_PLAYER_NAME_LENGTH+1);
	size_t next_char_index = 0;
	char last_char = 0;
	if(!failure){
		do{
			/* Get a request message. */
			if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) {
				printf("driver_receive failed with: %d", r);
				continue;
			}
			if (is_ipc_notify(ipc_status)) { /* received notification */
				switch (_ENDPOINT_P(msg.m_source)) {
				case HARDWARE: /* hardware interrupt notification */
					if (msg.NOTIFY_ARG & get_com1_irq_set()) { /* subscribed interrupt */
						if(ser_ih(COM1))
							failure = 1;
					}
					if (msg.NOTIFY_ARG & get_kbd_irq_set()){  /* subscribed kbd interrupt */
						kbd_int_handler();
						if(get_scancode() == ESC_BREAK)
						{
							lobby_state = NOT_READY;
							fade_condition = true;
							transmit_message(MES_BYE);
						}
						if(get_role() == SERVER || get_role() == CLIENT)
						{
							if(get_scancode() == ENTER_MAKE && current_im_index != 0)
							{


								current_im_index = 0;
								transmit_protocol(current_im, TYPE_STRING, strlen(current_im)+1);
								char* temp = (char*) calloc(IM_MESSAGE_MAX + 4,1);
								strcpy(temp, "me: ");
								strcat(temp,current_im);

								push_back(im_message_log, &temp);

								memset(current_im, 0, IM_MESSAGE_MAX);
								memset(transmission_message,0,IM_MESSAGE_MAX+4);

							}
							else
							{
								int action;
								action = process_input(get_scancode(), &last_char);
								if(action == CHAR_RECEIVED)
								{
									if(current_im_index < IM_MESSAGE_MAX -1)
									{
										current_im[current_im_index] = last_char;
										current_im_index++;
									}
								}
								else if(action == BACKSPACE_MAKE)
								{
									if(current_im_index > 0)
									{
										current_im_index--;
										current_im[current_im_index] = '\0';
									}
								}
							}
						}
					}
					if (msg.NOTIFY_ARG & get_rtc_irq_set()) { /* subscribed timer interrupt */
						if(rtc_ih())
							failure = 1;
						getDateString(get_date_str_ptr());
					}
					if(msg.NOTIFY_ARG & get_timer_irq_set()){
						timer_int_handler();
						mp_lobby_render();
						//establish_connection_transmission();
					}
					if (msg.NOTIFY_ARG & get_mouse_irq_set()) { /* subscribed timer interrupt */
						mouse_int_handler();
					}
					break;
				default:
					break; /* no other notifications expected: do nothing */
				}
			} else {
			}
			establish_connection_reception();
			if(mouse_is_updated())
			{
				assign_mouse(get_previous_mouse(), get_mouse());
				assign_mouse(get_mouse(), get_mouse_state());
				if(get_role() == SERVER || get_role() == CLIENT)
					lobby_mouse_event(get_previous_mouse(), get_mouse());
				move_cursor(get_cursor(), get_mouse()->coords);
			}

		} while(continue_condition);
	}
	if(im_message_log != NULL)
	{
		size_t del_i;
		for(del_i = 0 ; del_i < size(im_message_log); del_i++)
		{
			free(*(char**)at(im_message_log, del_i));
		}
		delete_vector_t(im_message_log);
		im_message_log = NULL;
	}
	if(generic_ser_unsubscribe_int_tx_rx(COM1))
	{//unsubscribe interrupts
		printf("ser_int_receive(): generic_ser_unsubscribe_int() failed \n");
		failure = 1;
	}
	disable_per_second_alarm();
	continue_condition = true;
	fade_condition = false;
	darken = FADE_MAX;
	return 0;
}