void CEntityDlg::OnCbnSelchangeComboClass()
{
    int index = comboClass.GetCurSel();
    if (index != LB_ERR) {
        CString str;
        comboClass.GetLBText(index, str);
        eclass_t *ent = Eclass_ForName (str, false);
        if (ent) {
            if (selected_brushes.next == &selected_brushes) {
                editEntity = world_entity;
                multipleEntities = false;
            } else {
                editEntity = selected_brushes.next->owner;
                for (brush_t *b = selected_brushes.next->next; b != &selected_brushes; b = b->next) {
                    if (b->owner != editEntity) {
                        multipleEntities = true;
                        break;
                    }
                }
            }
            listVars.ResetContent();
            CPropertyItem *pi = new CPropertyItem("Usage:", ent->desc.c_str(), PIT_VAR, "");
            listVars.AddPropItem(pi);

            int c = ent->vars.Num();
            for (int i = 0; i < c; i++) {
                pi = new CPropertyItem(ent->vars[i].name.c_str(), ent->vars[i].desc.c_str(), PIT_VAR, "");
                pi->SetData(ent->vars[i].type);
                listVars.AddPropItem(pi);
            }
            listVars.Invalidate();
            SetKeyValPairs();
        }
    }
}
void CEntityDlg::OnCbnAnimationChange ()
{
    if ( !editEntity )
    {
        return;
    }

    int sel = cbAnimations.GetCurSel();
    CString animName;
    currentAnimation = NULL;
    int currFrame = 0;

    if ( sel != -1 ) {
        cbAnimations.GetLBText( sel , animName );
        if ( animName.GetLength() > 0 ) {
            //preserve the existing frame number
            currFrame = editEntity->epairs.GetInt ( "frame" , "1" );

            editEntity->epairs.Set("anim" , animName.GetBuffer(0));
            SetKeyValPairs(false/*don't update anims combo box :)*/ );

            //update the slider
            currentAnimation = gameEdit->ANIM_GetAnimFromEntityDef(editEntity->eclass->name , animName.GetBuffer(0));
            currentAnimationFrame = 0;

            if ( currentAnimation ) {
                slFrameSlider.SetRange( 1 , gameEdit->ANIM_GetNumFrames( currentAnimation ), TRUE );
                slFrameSlider.SetPos( currFrame );
                currentAnimationFrame = currFrame;
            }

            Sys_UpdateWindows(W_ALL);
        }
    }
}
void CEntityDlg::UpdateKeyVal(const char *key, const char *val) {
    if (editEntity) {
        editEntity->epairs.Set(key, val);
        SetKeyValPairs();
        g_pParentWnd->GetCamera()->BuildEntityRenderState(editEntity, true);
        Entity_UpdateSoundEmitter(editEntity);
    }
}
void CEntityDlg::UpdateFromAnimationFrame( bool updateKeyValueDisplay ) {
	int frame = slFrameSlider.GetPos();
	editEntity->epairs.SetInt( "frame" , frame );
	SetDlgItemText( IDC_ENTITY_CURRENT_ANIM , va( "%i" , frame ) );
	if( updateKeyValueDisplay ) {
		SetKeyValPairs();
	}
	g_pParentWnd->GetCamera()->BuildEntityRenderState( editEntity , true );
	Sys_UpdateWindows( W_ALL );
}
void CEntityDlg::UpdateFromListBox() {
    if (editEntity == NULL) {
        return;
    }
    int c = listKeyVal.GetCount();
    for (int i = 0 ; i < c; i++) {
        CPropertyItem* pItem = (CPropertyItem*)listKeyVal.GetItemDataPtr(i);
        if (pItem) {
            editEntity->epairs.Set(pItem->m_propName, pItem->m_curValue);
        }
    }
    SetKeyValPairs();
}
/*
 =======================================================================================================================
    AddProp
 =======================================================================================================================
 */
void CEntityDlg::AddProp() {
	if( editEntity == NULL ) {
		return;
	}
	CString Key, Value;
	editKey.GetWindowText( Key );
	editVal.GetWindowText( Value );
	bool isName = ( stricmp( Key, "name" ) == 0 );
	bool isModel = static_cast<bool>( ( stricmp( Key, "model" ) == 0 && Value.GetLength() > 0 ) );
	bool isOrigin = ( idStr::Icmp( Key, "origin" ) == 0 );
	if( multipleEntities ) {
		brush_t *b;
		for( b = selected_brushes.next; b != &selected_brushes; b = b->next ) {
			if( isName ) {
				Entity_SetName( b->owner, Value );
			} else {
				if( !( ( isModel || isOrigin ) && ( b->owner->eclass->nShowFlags & ECLASS_WORLDSPAWN ) ) ) {
					SetKeyValue( b->owner, Key, Value );
				}
			}
		}
	} else {
		if( isName ) {
			Entity_SetName( editEntity, Value );
		} else {
			if( !( ( isModel || isOrigin ) && ( editEntity->eclass->nShowFlags & ECLASS_WORLDSPAWN ) ) ) {
				SetKeyValue( editEntity, Key, Value );
			}
		}
		if( isModel && !( editEntity->eclass->nShowFlags & ECLASS_WORLDSPAWN ) ) {
			idBounds	bo;
			idVec3	mins, maxs;
			selected_brushes.next->modelHandle = renderModelManager->FindModel( Value );
			if( dynamic_cast<idRenderModelPrt *>( selected_brushes.next->modelHandle ) || dynamic_cast<idRenderModelLiquid *>( selected_brushes.next->modelHandle ) ) {
				bo.Zero();
				bo.ExpandSelf( 12.0f );
			} else {
				bo = selected_brushes.next->modelHandle->Bounds( NULL );
			}
			VectorCopy( bo[0], mins );
			VectorCopy( bo[1], maxs );
			VectorAdd( mins, editEntity->origin, mins );
			VectorAdd( maxs, editEntity->origin, maxs );
			Brush_RebuildBrush( selected_brushes.next, mins, maxs, false );
			Brush_Build( selected_brushes.next , false, false , false, true );
		}
	}
	// refresh the prop listbox
	SetKeyValPairs();
	Sys_UpdateWindows( W_ALL );
}
void CEntityDlg::DelProp() {
	CString key;
	if( editEntity == NULL ) {
		return;
	}
	editKey.GetWindowText( key );
	if( multipleEntities ) {
		for( brush_t *b = selected_brushes.next; b != &selected_brushes; b = b->next ) {
			DeleteKey( b->owner, key );
			Entity_UpdateCurveData( b->owner );
		}
	} else {
		DeleteKey( editEntity, key );
		Entity_UpdateCurveData( editEntity );
	}
	// refresh the prop listbox
	SetKeyValPairs();
	Sys_UpdateWindows( W_ENTITY | W_XY | W_CAMERA );
}
//
// =======================================================================================================================
//    CreateEntity Creates a new entity based on the currently selected brush and entity type.
// =======================================================================================================================
//
void CEntityDlg::CreateEntity() {
    entity_t	*petNew;
    bool		forceFixed = false;

    // check to make sure we have a brush
    CXYWnd	*pWnd = g_pParentWnd->ActiveXY();
    if (pWnd) {
        CRect	rctZ;
        pWnd->GetClientRect(rctZ);

        brush_t *pBrush;
        if (selected_brushes.next == &selected_brushes) {
            pBrush = CreateEntityBrush(g_nSmartX, rctZ.Height() - 1 - g_nSmartY, pWnd);
            forceFixed = true;
        }
    }
    else {
        if (selected_brushes.next == &selected_brushes) {
            MessageBox("You must have a selected brush to create an entity", "info", 0);
            return;
        }
    }

    int index = comboClass.GetCurSel();
    if (index == LB_ERR) {
        MessageBox("You must have a selected class to create an entity", "info", 0);
        return;
    }

    CString str;
    comboClass.GetLBText(index, str);

    if (!stricmp(str, "worldspawn")) {
        MessageBox("Can't create an entity with worldspawn.", "info", 0);
        return;
    }

    eclass_t *pecNew = Eclass_ForName (str, false);

    // create it
    if ((GetAsyncKeyState(VK_CONTROL) & 0x8000)) {
        // MAJOR hack for xian
        extern void Brush_CopyList(brush_t *pFrom, brush_t *pTo);
        brush_t temp_brushes;
        temp_brushes.next = &temp_brushes;
        Brush_CopyList(&selected_brushes, &temp_brushes);
        Select_Deselect();
        brush_t *pBrush = temp_brushes.next;
        while (pBrush != NULL && pBrush != &temp_brushes) {
            brush_t *pNext = pBrush->next;
            Brush_RemoveFromList(pBrush);
            Brush_AddToList(pBrush, &selected_brushes);
            pBrush = pNext;
            petNew = Entity_Create(pecNew, forceFixed);
            Select_Deselect();
        }
    } else if ((GetAsyncKeyState(VK_SHIFT) & 0x8000)) {
        Select_Ungroup();
        petNew = Entity_Create(pecNew, forceFixed);
    } else {
        petNew = Entity_Create(pecNew, forceFixed);
    }

    if (petNew == NULL) {
        MessageBox("Failed to create entity.", "info", 0);
        return;
    }

    if (selected_brushes.next == &selected_brushes) {
        editEntity = world_entity;
    }
    else {
        editEntity = selected_brushes.next->owner;
    }

    SetKeyValPairs();
    Select_Deselect();
    Select_Brush(editEntity->brushes.onext);
    Sys_UpdateWindows(W_ALL);
}
BOOL CEntityDlg::PreTranslateMessage(MSG* pMsg)
{
// ---> sikk - Added
    //if ( pMsg->message == WM_LBUTTONDOWN || pMsg->message == WM_RBUTTONDOWN || pMsg->message == WM_MBUTTONDOWN ) {
    //	g_Inspectors->BringWindowToTop();
    //}
// <--- sikk - Added

    if (pMsg->hwnd == editVal.GetSafeHwnd()) {
        if (pMsg->message == WM_LBUTTONDOWN) {
            editVal.SetFocus();
//			return TRUE;	// sikk - Don't return so we can do default message handling
        }
    }

    if (pMsg->hwnd == editKey.GetSafeHwnd()) {
        if (pMsg->message == WM_LBUTTONDOWN) {
            editKey.SetFocus();
//			return TRUE;	// sikk - Don't return so we can do default message handling
        }
    }

    if (GetFocus() == &editVal || GetFocus() == &editKey) {
        if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN ) {
            AddProp();
            return TRUE;
        }

    }

    if (GetFocus() == listKeyVal.GetEditBox()) {
        if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN ) {
            listKeyVal.OnChangeEditBox();
            listKeyVal.OnSelchange();
            listKeyVal.OnKillfocusEditBox();
            AddProp();
            SetKeyValPairs();
            return TRUE;
        }
    }

    if (GetFocus() == &listKeyVal) {
        if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DELETE && editEntity) {
            DelProp();
            return TRUE;
        }
    }

    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) {
        if (pMsg->wParam == VK_ESCAPE) {
            g_pParentWnd->GetCamera()->SetFocus();
            Select_Deselect();
        }
        return TRUE;
    }

    if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN ) {
        // keeps ENTER from closing the dialog
        return TRUE;
    }

    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_TAB) {
        if (GetFocus()) {
            int id = GetFocus()->GetDlgCtrlID();
            for (int i = 0; i < TabCount; i++) {
                if (TabOrder[i] == id) {
                    i++;
                    if (i >= TabCount) {
                        i = 0;
                    }
                    CWnd *next = GetDlgItem(TabOrder[i]);
                    if (next) {
                        next->SetFocus();
                        if (TabOrder[i] == IDC_EDIT_VAL) {
                            editVal.SetSel(0, -1);
                        }
                        return TRUE;
                    }
                }
            }
        }
    }

    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RIGHT && pMsg->hwnd == slFrameSlider.GetSafeHwnd()) {
        int pos = slFrameSlider.GetPos() + 1;
        pos = (pos % slFrameSlider.GetRangeMax());
        slFrameSlider.SetPos ( pos );
        UpdateFromAnimationFrame ();
        return TRUE;
    }

    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_LEFT && pMsg->hwnd == slFrameSlider.GetSafeHwnd()) {
        int pos = slFrameSlider.GetPos() - 1;

        if ( pos < 1 ) {
            pos = slFrameSlider.GetRangeMax();
        }

        slFrameSlider.SetPos ( pos );
        UpdateFromAnimationFrame ();
        return TRUE;
    }

    return CDialog::PreTranslateMessage(pMsg);
}